
<span id="io"></span><h1><span class="yiyi-st" id="yiyi-176">IO Tools (Text, CSV, HDF5, ...)</span></h1>
        <blockquote>
        <p>原文：<a href="http://pandas.pydata.org/pandas-docs/stable/io.html">http://pandas.pydata.org/pandas-docs/stable/io.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<p><span class="yiyi-st" id="yiyi-177">pandas I / O API是一组顶级的<code class="docutils literal"><span class="pre">reader</span></code>函数，像<code class="docutils literal"><span class="pre">pd.read_csv()</span></code>访问，通常返回一个<code class="docutils literal"><span class="pre">pandas</span></code>对象。</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-178"><a class="reference internal" href="#io-read-csv-table"><span class="std std-ref">read_csv</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-179"><a class="reference internal" href="#io-excel-reader"><span class="std std-ref">read_excel</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-180"><a class="reference internal" href="#io-hdf5"><span class="std std-ref">read_hdf</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-181"><a class="reference internal" href="#io-sql"><span class="std std-ref">read_sql</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-182"><a class="reference internal" href="#io-json-reader"><span class="std std-ref">read_json</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-183"><a class="reference internal" href="#io-msgpack"><span class="std std-ref">read_msgpack</span></a>（实验性）</span></li>
<li><span class="yiyi-st" id="yiyi-184"><a class="reference internal" href="#io-read-html"><span class="std std-ref">read_html</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-185"><a class="reference internal" href="#io-bigquery-reader"><span class="std std-ref">read_gbq</span></a>（实验性）</span></li>
<li><span class="yiyi-st" id="yiyi-186"><a class="reference internal" href="#io-stata-reader"><span class="std std-ref">read_stata</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-187"><a class="reference internal" href="#io-sas-reader"><span class="std std-ref">read_sas</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-188"><a class="reference internal" href="#io-clipboard"><span class="std std-ref">read_clipboard</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-189"><a class="reference internal" href="#io-pickle"><span class="std std-ref">read_pickle</span></a></span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-190">对应的<code class="docutils literal"><span class="pre">writer</span></code>函数是对象方法，像<code class="docutils literal"><span class="pre">df.to_csv()</span></code></span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-191"><a class="reference internal" href="#io-store-in-csv"><span class="std std-ref">to_csv</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-192"><a class="reference internal" href="#io-excel-writer"><span class="std std-ref">to_excel</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-193"><a class="reference internal" href="#io-hdf5"><span class="std std-ref">to_hdf</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-194"><a class="reference internal" href="#io-sql"><span class="std std-ref">to_sql</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-195"><a class="reference internal" href="#io-json-writer"><span class="std std-ref">to_json</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-196"><a class="reference internal" href="#io-msgpack"><span class="std std-ref">to_msgpack</span></a>（实验性）</span></li>
<li><span class="yiyi-st" id="yiyi-197"><a class="reference internal" href="#io-html"><span class="std std-ref">to_html</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-198"><a class="reference internal" href="#io-bigquery-writer"><span class="std std-ref">to_gbq</span></a>（实验）</span></li>
<li><span class="yiyi-st" id="yiyi-199"><a class="reference internal" href="#io-stata-writer"><span class="std std-ref">to_stata</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-200"><a class="reference internal" href="#io-clipboard"><span class="std std-ref">to_clipboard</span></a></span></li>
<li><span class="yiyi-st" id="yiyi-201"><a class="reference internal" href="#io-pickle"><span class="std std-ref">to_pickle</span></a></span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-202"><a class="reference internal" href="#io-perf"><span class="std std-ref">Here</span></a>是其中一些IO方法的非正式性能比较。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-203">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-204">For examples that use the <code class="docutils literal"><span class="pre">StringIO</span></code> class, make sure you import it according to your Python version, i.e. <code class="docutils literal"><span class="pre">from</span> <span class="pre">StringIO</span> <span class="pre">import</span> <span class="pre">StringIO</span></code> for Python 2 and <code class="docutils literal"><span class="pre">from</span> <span class="pre">io</span> <span class="pre">import</span> <span class="pre">StringIO</span></code> for Python 3.</span></p>
</div>
<div class="section" id="csv-text-files">
<span id="io-read-csv-table"></span><h2><span class="yiyi-st" id="yiyi-205">CSV &amp; Text files</span></h2>
<p><span class="yiyi-st" id="yiyi-206">用于读取文本文件的两个主力功能（a.k.a.</span><span class="yiyi-st" id="yiyi-207">平面文件）是<a class="reference internal" href="generated/pandas.read_csv.html#pandas.read_csv" title="pandas.read_csv"><code class="xref py py-func docutils literal"><span class="pre">read_csv()</span></code></a>和<a class="reference internal" href="generated/pandas.read_table.html#pandas.read_table" title="pandas.read_table"><code class="xref py py-func docutils literal"><span class="pre">read_table()</span></code></a>。</span><span class="yiyi-st" id="yiyi-208">他们都使用相同的解析代码智能地将表格数据转换为DataFrame对象。</span><span class="yiyi-st" id="yiyi-209">有关某些高级策略，请参阅<a class="reference internal" href="cookbook.html#cookbook-csv"><span class="std std-ref">cookbook</span></a>。</span></p>
<div class="section" id="parsing-options">
<h3><span class="yiyi-st" id="yiyi-210">Parsing options</span></h3>
<p><span class="yiyi-st" id="yiyi-211"><a class="reference internal" href="generated/pandas.read_csv.html#pandas.read_csv" title="pandas.read_csv"><code class="xref py py-func docutils literal"><span class="pre">read_csv()</span></code></a>和<a class="reference internal" href="generated/pandas.read_table.html#pandas.read_table" title="pandas.read_table"><code class="xref py py-func docutils literal"><span class="pre">read_table()</span></code></a></span></p>
<div class="section" id="basic">
<h4><span class="yiyi-st" id="yiyi-212">Basic</span></h4>
<dl class="docutils">
<dt><span class="yiyi-st" id="yiyi-213">filepath_or_buffer</span></dt><span class="yiyi-st" id="yiyi-227"><span class="classifier-delimiter">：</span> <span class="classifier">各种</span></span><dd><span class="yiyi-st" id="yiyi-214">要么是文件的路径（<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.6)"><code class="docutils literal"><span class="pre">str</span></code></a>，<a class="reference external" href="https://docs.python.org/3/library/pathlib.html#pathlib.Path" title="(in Python v3.6)"><code class="docutils literal"><span class="pre">pathlib.Path</span></code></a>或<code class="xref py py-class docutils literal"><span class="pre">py._path.local.LocalPath</span></code>），URL（包括http， ftp和S3位置），或任何具有<code class="docutils literal"><span class="pre">read()</span></code>方法（例如打开的文件或<a class="reference external" href="https://docs.python.org/3/library/io.html#io.StringIO" title="(in Python v3.6)"><code class="xref py py-class docutils literal"><span class="pre">StringIO</span></code></a>）的对象。</span></dd>
<dt><span class="yiyi-st" id="yiyi-215">sep</span></dt><span class="yiyi-st" id="yiyi-228"> <span class="classifier-delimiter">:</span> <span class="classifier">str, defaults to <code class="docutils literal"><span class="pre">&apos;,&apos;</span></code> for <a class="reference internal" href="generated/pandas.read_csv.html#pandas.read_csv" title="pandas.read_csv"><code class="xref py py-func docutils literal"><span class="pre">read_csv()</span></code></a>, <code class="docutils literal"><span class="pre">\t</span></code> for <a class="reference internal" href="generated/pandas.read_table.html#pandas.read_table" title="pandas.read_table"><code class="xref py py-func docutils literal"><span class="pre">read_table()</span></code></a></span></span><dd><span class="yiyi-st" id="yiyi-216">分隔符使用。</span><span class="yiyi-st" id="yiyi-217">如果sep为<code class="docutils literal"><span class="pre">None</span></code>，将尝试自动确定这一点。</span><span class="yiyi-st" id="yiyi-218">长度大于1个字符且与<code class="docutils literal"><span class="pre">&apos;\s+&apos;</span></code>不同的分隔符将被解释为正则表达式，将强制使用Python解析引擎，并忽略数据中的引号。</span><span class="yiyi-st" id="yiyi-219">正则表达式示例：<code class="docutils literal"><span class="pre">&apos;\\r\\t&apos;</span></code>。</span></dd>
<dt><span class="yiyi-st" id="yiyi-220">delimiter</span></dt><span class="yiyi-st" id="yiyi-229"><span class="classifier-delimiter">：</span> <span class="classifier">str，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-221">sep的备用参数名称。</span></dd>
<dt><span class="yiyi-st" id="yiyi-222">delim_whitespace</span></dt><span class="yiyi-st" id="yiyi-230"><span class="classifier-delimiter">：</span> <span class="classifier">boolean，default False</span></span><dd><p class="first"><span class="yiyi-st" id="yiyi-223">指定是否使用空格（例如<code class="docutils literal"><span class="pre">&apos;</span> <span class="pre">&apos;</span></code>或<code class="docutils literal"><span class="pre">&apos;\t&apos;</span></code>）作为分隔符。</span><span class="yiyi-st" id="yiyi-224">相当于设置<code class="docutils literal"><span class="pre">sep=&apos;\s+&apos;</span></code>。</span><span class="yiyi-st" id="yiyi-225">如果此选项设置为True，则不应为<code class="docutils literal"><span class="pre">delimiter</span></code>参数传入任何内容。</span></p>
<div class="last versionadded">
<p><span class="yiyi-st" id="yiyi-226"><span class="versionmodified">版本0.18.1中的新功能：</span>支持Python解析器。</span></p>
</div>
</dd>
</dl>
</div>
<div class="section" id="column-and-index-locations-and-names">
<h4><span class="yiyi-st" id="yiyi-231">Column and Index Locations and Names</span></h4>
<dl class="docutils">
<dt><span class="yiyi-st" id="yiyi-232">标题</span></dt><span class="yiyi-st" id="yiyi-265"><span class="classifier-delimiter">：</span> <span class="classifier">int或ints列表，默认<code class="docutils literal"><span class="pre">&apos;infer&apos;</span></code></span></span><dd><span class="yiyi-st" id="yiyi-233">要用作列名称的行号，以及数据的开始。</span><span class="yiyi-st" id="yiyi-234">如果没有传递<code class="docutils literal"><span class="pre">names</span></code>，默认行为就像<code class="docutils literal"><span class="pre">header=0</span></code>，否则就像<code class="docutils literal"><span class="pre">header=None</span></code>。</span><span class="yiyi-st" id="yiyi-235">显式传递<code class="docutils literal"><span class="pre">header=0</span></code>，以便能够替换现有名称。</span><span class="yiyi-st" id="yiyi-236">头部可以是指定列的多索引的行位置的整数列表，例如<code class="docutils literal"><span class="pre">[0,1,3]</span></code>。</span><span class="yiyi-st" id="yiyi-237">未指定的插入行将被跳过（例如，在此示例中跳过2）。</span><span class="yiyi-st" id="yiyi-238">请注意，如果<code class="docutils literal"><span class="pre">skip_blank_lines=True</span></code>，此参数将忽略已注释的行和空行，因此header = 0表示数据的第一行，而不是文件的第一行。</span></dd>
<dt><span class="yiyi-st" id="yiyi-239">名称</span></dt><span class="yiyi-st" id="yiyi-266"><span class="classifier-delimiter">：</span> <span class="classifier">数组样，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-240">要使用的列名称列表。</span><span class="yiyi-st" id="yiyi-241">如果文件不包含标题行，则应明确传递<code class="docutils literal"><span class="pre">header=None</span></code>。</span><span class="yiyi-st" id="yiyi-242">除非<code class="docutils literal"><span class="pre">mangle_dupe_cols=True</span></code>，此列表中的重复项是不允许的，这是默认值。</span></dd>
<dt><span class="yiyi-st" id="yiyi-243">index_col</span></dt><span class="yiyi-st" id="yiyi-267"><span class="classifier-delimiter">：</span> <span class="classifier">int或序列或<code class="docutils literal"><span class="pre">False</span></code>，默认<code class="docutils literal"><span class="pre">None</span></code> </span></span><dd><span class="yiyi-st" id="yiyi-244">用作DataFrame的行标签的列。</span><span class="yiyi-st" id="yiyi-245">如果给出序列，则使用MultiIndex。</span><span class="yiyi-st" id="yiyi-246">如果您在每行末尾都有带分隔符的格式不正确的文件，则可以考虑<code class="docutils literal"><span class="pre">index_col=False</span></code>强制将pandas强制为<em>而不是</em>使用第一列作为索引）。</span></dd>
<dt><span class="yiyi-st" id="yiyi-247">usecols</span></dt><span class="yiyi-st" id="yiyi-268"><span class="classifier-delimiter">：</span> <span class="classifier">数组样，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-248">返回列的子集。</span><span class="yiyi-st" id="yiyi-249">此数组中的所有元素必须是位置（即，文档列中的整数索引）或对应于用户在<cite>名称</cite>中提供或从文档标题行推断的列名称的字符串。</span><span class="yiyi-st" id="yiyi-250">例如，有效的<cite>usecols</cite>参数将是[0，1，2]或[&apos;foo&apos;，&apos;bar&apos;，&apos;baz&apos;]。</span><span class="yiyi-st" id="yiyi-251">使用此参数会导致更快的解析时间和更低的内存使用率。</span></dd>
<dt><span class="yiyi-st" id="yiyi-252">as_recarray</span></dt><span class="yiyi-st" id="yiyi-269"><span class="classifier-delimiter">：</span> <span class="classifier">boolean，默认<code class="docutils literal"><span class="pre">False</span></code></span></span><dd><p class="first"><span class="yiyi-st" id="yiyi-253">DEPRECATED：此参数将在以后的版本中删除。</span><span class="yiyi-st" id="yiyi-254">请改用<code class="docutils literal"><span class="pre">pd.read_csv(...).to_records()</span></code>。</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-255">在解析数据后，返回NumPy recarray而不是DataFrame。</span><span class="yiyi-st" id="yiyi-256">如果设置为<code class="docutils literal"><span class="pre">True</span></code>，则此选项优先于<code class="docutils literal"><span class="pre">squeeze</span></code>参数。</span><span class="yiyi-st" id="yiyi-257">此外，由于行索引在此类格式中不可用，因此将忽略<code class="docutils literal"><span class="pre">index_col</span></code>参数。</span></p>
</dd>
<dt><span class="yiyi-st" id="yiyi-258">挤</span></dt><span class="yiyi-st" id="yiyi-270"><span class="classifier-delimiter">：</span> <span class="classifier">boolean，默认<code class="docutils literal"><span class="pre">False</span></code></span></span><dd><span class="yiyi-st" id="yiyi-259">如果解析的数据只包含一列，则返回一个Series。</span></dd>
<dt><span class="yiyi-st" id="yiyi-260">字首</span></dt><span class="yiyi-st" id="yiyi-271"><span class="classifier-delimiter">：</span> <span class="classifier">str，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-261">在没有标题时添加到列号的前缀，例如&apos;X&apos;代表X0，X1，...</span></dd>
<dt><span class="yiyi-st" id="yiyi-262">mangle_dupe_cols</span></dt><span class="yiyi-st" id="yiyi-272"><span class="classifier-delimiter">：</span> <span class="classifier">布尔值，默认<code class="docutils literal"><span class="pre">True</span></code></span></span><dd><span class="yiyi-st" id="yiyi-263">重复的列将被指定为“X.0”...“X.N”，而不是“X”...“X”。</span><span class="yiyi-st" id="yiyi-264">如果在列中存在重复的名称，则传入False将导致覆盖数据。</span></dd>
</dl>
</div>
<div class="section" id="general-parsing-configuration">
<h4><span class="yiyi-st" id="yiyi-273">General Parsing Configuration</span></h4>
<dl class="docutils">
<dt><span class="yiyi-st" id="yiyi-274">dtype</span></dt><span class="yiyi-st" id="yiyi-315"><span class="classifier-delimiter">：</span> <span class="classifier">输入列的名称或字典 - ＆gt；类型，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-275">数据或列的数据类型。</span><span class="yiyi-st" id="yiyi-276">例如。 <code class="docutils literal"><span class="pre">{&apos;a&apos;：</span> <span class="pre">np.float64，</span> <span class="pre">&apos;b&apos;：</span> <span class="pre">np.int32}  t0&gt;（不支持<code class="docutils literal"><span class="pre">engine=&apos;python&apos;</span></code>）。</span></code></span><span class="yiyi-st" id="yiyi-277">使用<cite>str</cite>或<cite>对象</cite>来保留而不是解释dtype。</span></dd>
<dt><span class="yiyi-st" id="yiyi-278">驱动</span></dt><span class="yiyi-st" id="yiyi-316"><span class="classifier-delimiter">：</span> <span class="classifier">{<code class="docutils literal"><span class="pre">&apos;c&apos;</span></code>，<code class="docutils literal"><span class="pre">&apos;python&apos;</span></code>}</span></span><dd><span class="yiyi-st" id="yiyi-279">解析器引擎使用。</span><span class="yiyi-st" id="yiyi-280">C引擎速度更快，而python引擎目前更加完善。</span></dd>
<dt><span class="yiyi-st" id="yiyi-281">转换器</span></dt><span class="yiyi-st" id="yiyi-317"><span class="classifier-delimiter">：</span> <span class="classifier">dict，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-282">说明转换某些列中的值的函数。</span><span class="yiyi-st" id="yiyi-283">键可以是整数或列标签。</span></dd>
<dt><span class="yiyi-st" id="yiyi-284">true_values</span></dt><span class="yiyi-st" id="yiyi-318"><span class="classifier-delimiter">：</span> <span class="classifier">列表，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-285">要考虑的值为<code class="docutils literal"><span class="pre">True</span></code>。</span></dd>
<dt><span class="yiyi-st" id="yiyi-286">false_values</span></dt><span class="yiyi-st" id="yiyi-319"><span class="classifier-delimiter">：</span> <span class="classifier">列表，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-287">要考虑的值为<code class="docutils literal"><span class="pre">False</span></code>。</span></dd>
<dt><span class="yiyi-st" id="yiyi-288">skipinitialspace</span></dt><span class="yiyi-st" id="yiyi-320"><span class="classifier-delimiter">：</span> <span class="classifier">boolean，默认<code class="docutils literal"><span class="pre">False</span></code></span></span><dd><span class="yiyi-st" id="yiyi-289">跳过分隔符后的空格。</span></dd>
<dt><span class="yiyi-st" id="yiyi-290">skiprows</span></dt><span class="yiyi-st" id="yiyi-321"><span class="classifier-delimiter">：</span> <span class="classifier">列表状或整数，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-291">要跳过的行号（0索引）或要跳过的行数（int）在文件的开头。</span></dd>
<dt><span class="yiyi-st" id="yiyi-292">skipfooter</span></dt><span class="yiyi-st" id="yiyi-322"><span class="classifier-delimiter">：</span> <span class="classifier">int，默认<code class="docutils literal"><span class="pre">0</span></code></span></span><dd><span class="yiyi-st" id="yiyi-293">要跳过的文件底部的行数（不支持engine =&apos;c&apos;）。</span></dd>
<dt><span class="yiyi-st" id="yiyi-294">skip_footer</span></dt><span class="yiyi-st" id="yiyi-323"><span class="classifier-delimiter">：</span> <span class="classifier">int，默认<code class="docutils literal"><span class="pre">0</span></code></span></span><dd><span class="yiyi-st" id="yiyi-295">DEPRECATED：使用<code class="docutils literal"><span class="pre">skipfooter</span></code>参数，因为它们是相同的</span></dd>
<dt><span class="yiyi-st" id="yiyi-296">nrows</span></dt><span class="yiyi-st" id="yiyi-324"><span class="classifier-delimiter">：</span> <span class="classifier">int，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-297">要读取的文件的行数。</span><span class="yiyi-st" id="yiyi-298">适用于读取大文件的片段。</span></dd>
<dt><span class="yiyi-st" id="yiyi-299">内存不足</span></dt><span class="yiyi-st" id="yiyi-325"><span class="classifier-delimiter">：</span> <span class="classifier">布尔值，默认<code class="docutils literal"><span class="pre">True</span></code></span></span><dd><span class="yiyi-st" id="yiyi-300">在内部以块的方式处理文件，导致解析时内存使用较少，但可能是混合类型推断。</span><span class="yiyi-st" id="yiyi-301">要确保没有混合类型，请设置<code class="docutils literal"><span class="pre">False</span></code>，或使用<code class="docutils literal"><span class="pre">dtype</span></code>参数指定类型。</span><span class="yiyi-st" id="yiyi-302">请注意，无论如何，整个文件都读入单个DataFrame，请使用<code class="docutils literal"><span class="pre">chunksize</span></code>或<code class="docutils literal"><span class="pre">iterator</span></code>参数以块形式返回数据。</span><span class="yiyi-st" id="yiyi-303">（只有C解析器有效）</span></dd>
<dt><span class="yiyi-st" id="yiyi-304">buffer_lines</span></dt><span class="yiyi-st" id="yiyi-326"><span class="classifier-delimiter">：</span> <span class="classifier">int，默认无</span></span><dd><span class="yiyi-st" id="yiyi-305">DEPRECATED：此参数将在未来版本中删除，因为其值不受解析器的影响</span></dd>
<dt><span class="yiyi-st" id="yiyi-306">compact_ints</span></dt><span class="yiyi-st" id="yiyi-327"><span class="classifier-delimiter">：</span> <span class="classifier">boolean，default False</span></span><dd><p class="first"><span class="yiyi-st" id="yiyi-307">DEPRECATED：此参数将在以后的版本中删除</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-308">如果<code class="docutils literal"><span class="pre">compact_ints</span></code>是<code class="docutils literal"><span class="pre">True</span></code>，则对于任何整数为dtype的列，解析器将尝试将其作为最小整数<code class="docutils literal"><span class="pre">dtype</span></code>根据<code class="docutils literal"><span class="pre">use_unsigned</span></code>参数的规范，可以是有符号或无符号。</span></p>
</dd>
<dt><span class="yiyi-st" id="yiyi-309">use_unsigned</span></dt><span class="yiyi-st" id="yiyi-328"><span class="classifier-delimiter">：</span> <span class="classifier">boolean，default False</span></span><dd><p class="first"><span class="yiyi-st" id="yiyi-310">DEPRECATED：此参数将在以后的版本中删除</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-311">如果整数列被压缩（即<code class="docutils literal"><span class="pre">compact_ints=True</span></code>），请指定该列是否应压缩到最小有符号或无符号整数dtype。</span></p>
</dd>
<dt><span class="yiyi-st" id="yiyi-312">memory_map</span></dt><span class="yiyi-st" id="yiyi-329"><span class="classifier-delimiter">：</span> <span class="classifier">boolean，default False</span></span><dd><span class="yiyi-st" id="yiyi-313">如果为<code class="docutils literal"><span class="pre">filepath_or_buffer</span></code>提供了文件路径，则将文件对象直接映射到内存上，并从中直接访问数据。</span><span class="yiyi-st" id="yiyi-314">使用此选项可以提高性能，因为不再有任何I / O开销。</span></dd>
</dl>
</div>
<div class="section" id="na-and-missing-data-handling">
<h4><span class="yiyi-st" id="yiyi-330">NA and Missing Data Handling</span></h4>
<dl class="docutils">
<dt><span class="yiyi-st" id="yiyi-331">na_values</span></dt><span class="yiyi-st" id="yiyi-344"><span class="classifier-delimiter">：</span> <span class="classifier">标量，str，列表式或dict，默认<code class="docutils literal"><span class="pre">None</span></code> </span></span><dd><span class="yiyi-st" id="yiyi-332">可识别为NA / NaN的其他字符串。</span><span class="yiyi-st" id="yiyi-333">如果dict通过，特定的每列NA值。</span><span class="yiyi-st" id="yiyi-334">By default the following values are interpreted as NaN: <code class="docutils literal"><span class="pre">&apos;-1.#IND&apos;,</span> <span class="pre">&apos;1.#QNAN&apos;,</span> <span class="pre">&apos;1.#IND&apos;,</span> <span class="pre">&apos;-1.#QNAN&apos;,</span> <span class="pre">&apos;#N/A</span> <span class="pre">N/A&apos;,</span> <span class="pre">&apos;#N/A&apos;,</span> <span class="pre">&apos;N/A&apos;,</span> <span class="pre">&apos;NA&apos;,</span> <span class="pre">&apos;#NA&apos;,</span> <span class="pre">&apos;NULL&apos;,</span> <span class="pre">&apos;NaN&apos;,</span> <span class="pre">&apos;-NaN&apos;,</span> <span class="pre">&apos;nan&apos;,</span> <span class="pre">&apos;-nan&apos;,</span> <span class="pre">&apos;&apos;</span></code>.</span></dd>
<dt><span class="yiyi-st" id="yiyi-335">keep_default_na</span></dt><span class="yiyi-st" id="yiyi-345"><span class="classifier-delimiter">：</span> <span class="classifier">布尔值，默认<code class="docutils literal"><span class="pre">True</span></code></span></span><dd><span class="yiyi-st" id="yiyi-336">如果指定了na_values且keep_default_na为<code class="docutils literal"><span class="pre">False</span></code>，则将覆盖默认NaN值，否则将追加到。</span></dd>
<dt><span class="yiyi-st" id="yiyi-337">na_filter</span></dt><span class="yiyi-st" id="yiyi-346"><span class="classifier-delimiter">：</span> <span class="classifier">布尔值，默认<code class="docutils literal"><span class="pre">True</span></code></span></span><dd><span class="yiyi-st" id="yiyi-338">检测缺失值标记（空字符串和na_values的值）。</span><span class="yiyi-st" id="yiyi-339">在没有任何NA的数据中，传递<code class="docutils literal"><span class="pre">na_filter=False</span></code>可以提高读取大文件的性能。</span></dd>
<dt><span class="yiyi-st" id="yiyi-340">详细</span></dt><span class="yiyi-st" id="yiyi-347"><span class="classifier-delimiter">：</span> <span class="classifier">boolean，默认<code class="docutils literal"><span class="pre">False</span></code></span></span><dd><span class="yiyi-st" id="yiyi-341">指示放置在非数字列中的NA值的数量。</span></dd>
<dt><span class="yiyi-st" id="yiyi-342">skip_blank_lines</span></dt><span class="yiyi-st" id="yiyi-348"><span class="classifier-delimiter">：</span> <span class="classifier">布尔值，默认<code class="docutils literal"><span class="pre">True</span></code></span></span><dd><span class="yiyi-st" id="yiyi-343">如果<code class="docutils literal"><span class="pre">True</span></code>，则跳过空白行，而不是解释为NaN值。</span></dd>
</dl>
</div>
<div class="section" id="datetime-handling">
<h4><span class="yiyi-st" id="yiyi-349">Datetime Handling</span></h4>
<dl class="docutils">
<dt><span class="yiyi-st" id="yiyi-350">parse_dates</span></dt><span class="yiyi-st" id="yiyi-366"><span class="classifier-delimiter">：</span> <span class="classifier">布尔值或整数或名称列表或列表或dict列表，默认<code class="docutils literal"><span class="pre">False</span></code>。</span></span><dd><ul class="first last simple">
<li><span class="yiyi-st" id="yiyi-351">如果<code class="docutils literal"><span class="pre">True</span></code>  - &gt;尝试解析索引。</span></li>
<li><span class="yiyi-st" id="yiyi-352">如果<code class="docutils literal"><span class="pre">[1，</span> <span class="pre">2，</span> <span class="pre">3]</span></code>  - &gt;尝试将列1,2,3分别解析为单独的日期列。</span></li>
<li><span class="yiyi-st" id="yiyi-353">如果<code class="docutils literal"><span class="pre">[[1，</span> <span class="pre">3]]</span></code>  - &gt;合并列1和3并解析为单个日期列。</span></li>
<li><span class="yiyi-st" id="yiyi-354">如果<code class="docutils literal"><span class="pre">{&apos;foo&apos;</span> <span class="pre">：</span> <span class="pre">[1，</span> <span class="pre">3]}</span> 列1，3作为日期和调用结果&apos;foo&apos;。</code></span><span class="yiyi-st" id="yiyi-355">iso8601格式的日期存在快速路径。</span></li>
</ul>
</dd>
<dt><span class="yiyi-st" id="yiyi-356">infer_datetime_format</span></dt><span class="yiyi-st" id="yiyi-367"><span class="classifier-delimiter">：</span> <span class="classifier">boolean，默认<code class="docutils literal"><span class="pre">False</span></code></span></span><dd><span class="yiyi-st" id="yiyi-357">If <code class="docutils literal"><span class="pre">True</span></code> and parse_dates is enabled for a column, attempt to infer the datetime format to speed up the processing.</span></dd>
<dt><span class="yiyi-st" id="yiyi-358">keep_date_col</span></dt><span class="yiyi-st" id="yiyi-368"><span class="classifier-delimiter">：</span> <span class="classifier">boolean，默认<code class="docutils literal"><span class="pre">False</span></code></span></span><dd><span class="yiyi-st" id="yiyi-359">如果<code class="docutils literal"><span class="pre">True</span></code>和parse_dates指定合并多个列，则保留原始列。</span></dd>
<dt><span class="yiyi-st" id="yiyi-360">date_parser</span></dt><span class="yiyi-st" id="yiyi-369"><span class="classifier-delimiter">：</span> <span class="classifier">功能，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-361">用于将字符串列序列转换为datetime实例数组的函数。</span><span class="yiyi-st" id="yiyi-362">默认使用<code class="docutils literal"><span class="pre">dateutil.parser.parser</span></code>进行转换。</span><span class="yiyi-st" id="yiyi-363">Pandas将尝试以三种不同的方式调用date_parser，如果发生异常，则推进到下一个：1）将一个或多个数组（由parse_dates定义）作为参数传递； 2）将由parse_dates定义的列中的字符串值连接（逐行）到单个数组中，并传递；和3）对于每一行，使用一个或多个字符串（对应于由parse_dates定义的列）作为参数调用date_parser一次。</span></dd>
<dt><span class="yiyi-st" id="yiyi-364">日间</span></dt><span class="yiyi-st" id="yiyi-370"><span class="classifier-delimiter">：</span> <span class="classifier">boolean，默认<code class="docutils literal"><span class="pre">False</span></code></span></span><dd><span class="yiyi-st" id="yiyi-365">DD / MM格式日期，国际和欧洲格式。</span></dd>
</dl>
</div>
<div class="section" id="iteration">
<h4><span class="yiyi-st" id="yiyi-371">Iteration</span></h4>
<dl class="docutils">
<dt><span class="yiyi-st" id="yiyi-372">迭代器</span></dt><span class="yiyi-st" id="yiyi-377"><span class="classifier-delimiter">：</span> <span class="classifier">boolean，默认<code class="docutils literal"><span class="pre">False</span></code></span></span><dd><span class="yiyi-st" id="yiyi-373">返回<cite>TextFileReader</cite>对象以进行迭代或使用<code class="docutils literal"><span class="pre">get_chunk()</span></code>获取块。</span></dd>
<dt><span class="yiyi-st" id="yiyi-374">chunksize</span></dt><span class="yiyi-st" id="yiyi-378"><span class="classifier-delimiter">：</span> <span class="classifier">int，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-375">返回<cite>TextFileReader</cite>对象以进行迭代。</span><span class="yiyi-st" id="yiyi-376">请参阅下面的<a class="reference internal" href="#io-chunking"><span class="std std-ref">iterating and chunking</span></a>。</span></dd>
</dl>
</div>
<div class="section" id="quoting-compression-and-file-format">
<h4><span class="yiyi-st" id="yiyi-379">Quoting, Compression, and File Format</span></h4>
<dl class="docutils">
<dt><span class="yiyi-st" id="yiyi-380">压缩</span></dt><span class="yiyi-st" id="yiyi-422"> <span class="classifier-delimiter">:</span> <span class="classifier">{<code class="docutils literal"><span class="pre">&apos;infer&apos;</span></code>, <code class="docutils literal"><span class="pre">&apos;gzip&apos;</span></code>, <code class="docutils literal"><span class="pre">&apos;bz2&apos;</span></code>, <code class="docutils literal"><span class="pre">&apos;zip&apos;</span></code>, <code class="docutils literal"><span class="pre">&apos;xz&apos;</span></code>, <code class="docutils literal"><span class="pre">None</span></code>}, default <code class="docutils literal"><span class="pre">&apos;infer&apos;</span></code></span></span><dd><p class="first"><span class="yiyi-st" id="yiyi-381">用于磁盘上数据的即时解压缩。</span><span class="yiyi-st" id="yiyi-382">如果&apos;infer&apos;，则使用gzip，bz2，zip或xz，如果filepath_or_buffer是分别以&apos;.gz&apos;，&apos;.bz2&apos;，&apos;.zip&apos;或&apos;.xz&apos;结尾的字符串，否则不进行解压缩。</span><span class="yiyi-st" id="yiyi-383">如果使用&apos;zip&apos;，ZIP文件必须只包含一个要读入的数据文件。</span><span class="yiyi-st" id="yiyi-384">设置为<code class="docutils literal"><span class="pre">None</span></code>，表示无解压缩。</span></p>
<div class="last versionadded">
<p><span class="yiyi-st" id="yiyi-385"><span class="versionmodified">新版本0.18.1：</span>支持&apos;zip&apos;和&apos;xz&apos;压缩。</span></p>
</div>
</dd>
<dt><span class="yiyi-st" id="yiyi-386">数千</span></dt><span class="yiyi-st" id="yiyi-423"><span class="classifier-delimiter">：</span> <span class="classifier">str，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-387">千位分隔符。</span></dd>
<dt><span class="yiyi-st" id="yiyi-388">十进制</span></dt><span class="yiyi-st" id="yiyi-424"><span class="classifier-delimiter">：</span> <span class="classifier">str，default <code class="docutils literal"><span class="pre">&apos;.&apos;</span></code></span></span><dd><span class="yiyi-st" id="yiyi-389">识别为小数点的字符。</span><span class="yiyi-st" id="yiyi-390">例如。对欧洲数据使用​​<code class="docutils literal"><span class="pre">&apos;,&apos;</span></code>。</span></dd>
<dt><span class="yiyi-st" id="yiyi-391">float_precision</span></dt><span class="yiyi-st" id="yiyi-425"><span class="classifier-delimiter">：</span> <span class="classifier">字符串，默认为无</span></span><dd><span class="yiyi-st" id="yiyi-392">指定C引擎应该为浮点值使用哪个转换器。</span><span class="yiyi-st" id="yiyi-393">对于普通转换器，选项为<code class="docutils literal"><span class="pre">None</span></code>，对于高精度转换器，选项为<code class="docutils literal"><span class="pre">high</span></code>，对于往返转换器选项为<code class="docutils literal"><span class="pre">round_trip</span></code>。</span></dd>
<dt><span class="yiyi-st" id="yiyi-394">线性判定器</span></dt><span class="yiyi-st" id="yiyi-426"><span class="classifier-delimiter">：</span> <span class="classifier">str（长度1），默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-395">将文件拆分成行的字符。</span><span class="yiyi-st" id="yiyi-396">只有C解析器有效。</span></dd>
<dt><span class="yiyi-st" id="yiyi-397">匹配</span></dt><span class="yiyi-st" id="yiyi-427"><span class="classifier-delimiter">：</span> <span class="classifier">str（length 1）</span></span><dd><span class="yiyi-st" id="yiyi-398">用于表示带引号项目的开始和结束的字符。</span><span class="yiyi-st" id="yiyi-399">引号项可以包含分隔符，它将被忽略。</span></dd>
<dt><span class="yiyi-st" id="yiyi-400">引用</span></dt><span class="yiyi-st" id="yiyi-428"><span class="classifier-delimiter">：</span> <span class="classifier">int或<code class="docutils literal"><span class="pre">csv.QUOTE_*</span></code>实例，默认<code class="docutils literal"><span class="pre">0</span></code> </span></span><dd><span class="yiyi-st" id="yiyi-401">每个<code class="docutils literal"><span class="pre">csv.QUOTE_*</span></code>常量的控制字段引用行为。</span><span class="yiyi-st" id="yiyi-402">使用<code class="docutils literal"><span class="pre">QUOTE_MINIMAL</span></code>（0），<code class="docutils literal"><span class="pre">QUOTE_ALL</span></code>（1），<code class="docutils literal"><span class="pre">QUOTE_NONNUMERIC</span></code>（2）或<code class="docutils literal"><span class="pre">QUOTE_NONE</span></code> 。</span></dd>
<dt><span class="yiyi-st" id="yiyi-403">双引号</span></dt><span class="yiyi-st" id="yiyi-429"><span class="classifier-delimiter">：</span> <span class="classifier">布尔值，默认<code class="docutils literal"><span class="pre">True</span></code></span></span><dd><span class="yiyi-st" id="yiyi-404">When <code class="docutils literal"><span class="pre">quotechar</span></code> is specified and <code class="docutils literal"><span class="pre">quoting</span></code> is not <code class="docutils literal"><span class="pre">QUOTE_NONE</span></code>, indicate whether or not to interpret two consecutive <code class="docutils literal"><span class="pre">quotechar</span></code> elements <strong>inside</strong> a field as a single <code class="docutils literal"><span class="pre">quotechar</span></code> element.</span></dd>
<dt><span class="yiyi-st" id="yiyi-405">escapechar</span></dt><span class="yiyi-st" id="yiyi-430"><span class="classifier-delimiter">：</span> <span class="classifier">str（长度1），默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-406">引号时用于转义分隔符的单字符字符串为<code class="docutils literal"><span class="pre">QUOTE_NONE</span></code>。</span></dd>
<dt><span class="yiyi-st" id="yiyi-407">评论</span></dt><span class="yiyi-st" id="yiyi-431"><span class="classifier-delimiter">：</span> <span class="classifier">str，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-408">表示不应解析行的剩余部分。</span><span class="yiyi-st" id="yiyi-409">如果在行的开头找到，则该行将完全被忽略。</span><span class="yiyi-st" id="yiyi-410">此参数必须为单个字符。</span><span class="yiyi-st" id="yiyi-411">与空行一样（只要<code class="docutils literal"><span class="pre">skip_blank_lines=True</span></code>），完全注释的行就会被参数<cite>头</cite>忽略，而不会被<cite>skiprows</cite>忽略。</span><span class="yiyi-st" id="yiyi-412">例如，如果<code class="docutils literal"><span class="pre">comment=&apos;#&apos;</span></code>，用<cite>header = 0</cite>解析&apos;#empty \ na，b，c \ n1,2,3&apos; ，b，c&apos;被当作报头。</span></dd>
<dt><span class="yiyi-st" id="yiyi-413">编码</span></dt><span class="yiyi-st" id="yiyi-432"><span class="classifier-delimiter">：</span> <span class="classifier">str，默认<code class="docutils literal"><span class="pre">None</span></code></span></span><dd><span class="yiyi-st" id="yiyi-414">在读/写时用于UTF的编码（例如<code class="docutils literal"><span class="pre">&apos;utf-8&apos;</span></code>）。</span><span class="yiyi-st" id="yiyi-415"><a class="reference external" href="https://docs.python.org/3/library/codecs.html#standard-encodings">Python标准编码列表</a>。</span></dd>
<dt><span class="yiyi-st" id="yiyi-416">方言</span></dt><span class="yiyi-st" id="yiyi-433"><span class="classifier-delimiter">：</span> <span class="classifier">str或<a class="reference external" href="https://docs.python.org/3/library/csv.html#csv.Dialect" title="(in Python v3.6)"><code class="docutils literal"><span class="pre">csv.Dialect</span></code></a>实例，默认<code class="docutils literal"><span class="pre">None</span></code> </span></span><dd><span class="yiyi-st" id="yiyi-417">如果<code class="docutils literal"><span class="pre">None</span></code>默认为Excel方言。</span><span class="yiyi-st" id="yiyi-418">如果sep长于1个字符，则忽略。</span><span class="yiyi-st" id="yiyi-419">有关详细信息，请参阅<a class="reference external" href="https://docs.python.org/3/library/csv.html#csv.Dialect" title="(in Python v3.6)"><code class="docutils literal"><span class="pre">csv.Dialect</span></code></a>文档。</span></dd>
<dt><span class="yiyi-st" id="yiyi-420">tupleize_cols</span></dt><span class="yiyi-st" id="yiyi-434"><span class="classifier-delimiter">：</span> <span class="classifier">boolean，默认<code class="docutils literal"><span class="pre">False</span></code></span></span><dd><span class="yiyi-st" id="yiyi-421">将列上的元组列表保留为原样（默认是将列转换为MultiIndex）。</span></dd>
</dl>
</div>
<div class="section" id="error-handling">
<h4><span class="yiyi-st" id="yiyi-435">Error Handling</span></h4>
<dl class="docutils">
<dt><span class="yiyi-st" id="yiyi-436">error_bad_lines</span></dt><span class="yiyi-st" id="yiyi-442"><span class="classifier-delimiter">：</span> <span class="classifier">布尔值，默认<code class="docutils literal"><span class="pre">True</span></code></span></span><dd><span class="yiyi-st" id="yiyi-437">默认情况下，具有太多字段的行（例如，具有太多逗号的csv行）将引发异常，并且不会返回DataFrame。</span><span class="yiyi-st" id="yiyi-438">如果<code class="docutils literal"><span class="pre">False</span></code>，那么这些“坏行”将从返回的DataFrame中删除（仅对C解析器有效）。</span><span class="yiyi-st" id="yiyi-439">请参阅下面的<a class="reference internal" href="#io-bad-lines"><span class="std std-ref">bad lines</span></a>。</span></dd>
<dt><span class="yiyi-st" id="yiyi-440">warn_bad_lines</span></dt><span class="yiyi-st" id="yiyi-443"><span class="classifier-delimiter">：</span> <span class="classifier">布尔值，默认<code class="docutils literal"><span class="pre">True</span></code></span></span><dd><span class="yiyi-st" id="yiyi-441">如果error_bad_lines为<code class="docutils literal"><span class="pre">False</span></code>，而warn_bad_lines为<code class="docutils literal"><span class="pre">True</span></code>，则会输出每个“坏行”的警告（仅对C解析器有效）。</span></dd>
</dl>
<p><span class="yiyi-st" id="yiyi-444">考虑一个典型的CSV文件，在这种情况下，包含一些时间序列数据：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="k">print</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;foo.csv&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="go">date,A,B,C</span>
<span class="go">20090101,a,1,2</span>
<span class="go">20090102,b,3,4</span>
<span class="go">20090103,c,4,5</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-445"><cite>read_csv</cite>的默认值是创建具有简单编号行的DataFrame：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [2]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;foo.csv&apos;</span><span class="p">)</span>
<span class="gr">Out[2]: </span>
<span class="go">       date  A  B  C</span>
<span class="go">0  20090101  a  1  2</span>
<span class="go">1  20090102  b  3  4</span>
<span class="go">2  20090103  c  4  5</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-446">在索引数据的情况下，您可以传递要用作索引的列号或列名：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [3]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;foo.csv&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[3]: </span>
<span class="go">          A  B  C</span>
<span class="go">date             </span>
<span class="go">20090101  a  1  2</span>
<span class="go">20090102  b  3  4</span>
<span class="go">20090103  c  4  5</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [4]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;foo.csv&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="s1">&apos;date&apos;</span><span class="p">)</span>
<span class="gr">Out[4]: </span>
<span class="go">          A  B  C</span>
<span class="go">date             </span>
<span class="go">20090101  a  1  2</span>
<span class="go">20090102  b  3  4</span>
<span class="go">20090103  c  4  5</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-447">您还可以使用列列表创建层次索引：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [5]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;foo.csv&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="s1">&apos;A&apos;</span><span class="p">])</span>
<span class="gr">Out[5]: </span>
<span class="go">            B  C</span>
<span class="go">date     A      </span>
<span class="go">20090101 a  1  2</span>
<span class="go">20090102 b  3  4</span>
<span class="go">20090103 c  4  5</span>
</pre></div>
</div>
<p id="io-dialect"><span class="yiyi-st" id="yiyi-448"><code class="docutils literal"><span class="pre">dialect</span></code>关键字在指定文件格式时具有更大的灵活性。</span><span class="yiyi-st" id="yiyi-449">默认情况下，它使用Excel方言，但您可以指定方言名称或<a class="reference external" href="https://docs.python.org/3/library/csv.html#csv.Dialect" title="(in Python v3.6)"><code class="docutils literal"><span class="pre">csv.Dialect</span></code></a>实例。</span></p>
<p><span class="yiyi-st" id="yiyi-450">假设您有未封闭的引号的数据：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [6]: </span><span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="go">label1,label2,label3</span>
<span class="go">index1,&quot;a,c,e</span>
<span class="go">index2,b,d,f</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-451">默认情况下，<code class="docutils literal"><span class="pre">read_csv</span></code>使用Excel方言，并将双引号作为引号字符，这会导致它在找到换行符之前找到换行符时失败。</span></p>
<p><span class="yiyi-st" id="yiyi-452">我们可以使用<code class="docutils literal"><span class="pre">dialect</span></code>解决这个问题</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [7]: </span><span class="n">dia</span> <span class="o">=</span> <span class="n">csv</span><span class="o">.</span><span class="n">excel</span><span class="p">()</span>

<span class="gp">In [8]: </span><span class="n">dia</span><span class="o">.</span><span class="n">quoting</span> <span class="o">=</span> <span class="n">csv</span><span class="o">.</span><span class="n">QUOTE_NONE</span>

<span class="gp">In [9]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">dialect</span><span class="o">=</span><span class="n">dia</span><span class="p">)</span>
<span class="gr">Out[9]: </span>
<span class="go">       label1 label2 label3</span>
<span class="go">index1     &quot;a      c      e</span>
<span class="go">index2      b      d      f</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-453">所有的方言选项可以通过关键字参数单独指定：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [10]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;a,b,c~1,2,3~4,5,6&apos;</span>

<span class="gp">In [11]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">lineterminator</span><span class="o">=</span><span class="s1">&apos;~&apos;</span><span class="p">)</span>
<span class="gr">Out[11]: </span>
<span class="go">   a  b  c</span>
<span class="go">0  1  2  3</span>
<span class="go">1  4  5  6</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-454">另一个常用的方言选项是<code class="docutils literal"><span class="pre">skipinitialspace</span></code>，跳过分隔符后的任何空格：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [12]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;a, b, c</span><span class="se">\n</span><span class="s1">1, 2, 3</span><span class="se">\n</span><span class="s1">4, 5, 6&apos;</span>

<span class="gp">In [13]: </span><span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="go">a, b, c</span>
<span class="go">1, 2, 3</span>
<span class="go">4, 5, 6</span>

<span class="gp">In [14]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">skipinitialspace</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gr">Out[14]: </span>
<span class="go">   a  b  c</span>
<span class="go">0  1  2  3</span>
<span class="go">1  4  5  6</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-455">解析器每一次尝试“做正确的事情”，而不是非常脆弱。</span><span class="yiyi-st" id="yiyi-456">类型推理是一个相当大的交易。</span><span class="yiyi-st" id="yiyi-457">因此，如果列可以强制转换为整数dtype，而不改变内容，它将这样做。</span><span class="yiyi-st" id="yiyi-458">任何非数字列将通过作为对象dtype与其余的pandas对象。</span></p>
</div>
</div>
<div class="section" id="specifying-column-data-types">
<span id="io-dtypes"></span><h3><span class="yiyi-st" id="yiyi-459">Specifying column data types</span></h3>
<p><span class="yiyi-st" id="yiyi-460">从v0.10开始，可以指定整个DataFrame或单独列的数据类型：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [15]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;a,b,c</span><span class="se">\n</span><span class="s1">1,2,3</span><span class="se">\n</span><span class="s1">4,5,6</span><span class="se">\n</span><span class="s1">7,8,9&apos;</span>

<span class="gp">In [16]: </span><span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="go">a,b,c</span>
<span class="go">1,2,3</span>
<span class="go">4,5,6</span>
<span class="go">7,8,9</span>

<span class="gp">In [17]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">object</span><span class="p">)</span>

<span class="gp">In [18]: </span><span class="n">df</span>
<span class="gr">Out[18]: </span>
<span class="go">   a  b  c</span>
<span class="go">0  1  2  3</span>
<span class="go">1  4  5  6</span>
<span class="go">2  7  8  9</span>

<span class="gp">In [19]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
<span class="gr">Out[19]: </span><span class="s1">&apos;1&apos;</span>

<span class="gp">In [20]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;b&apos;</span><span class="p">:</span> <span class="nb">object</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">})</span>

<span class="gp">In [21]: </span><span class="n">df</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[21]: </span>
<span class="go">a      int64</span>
<span class="go">b     object</span>
<span class="go">c    float64</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-461">幸运的是，<code class="docutils literal"><span class="pre">pandas</span></code>提供了多种方法来确保您的列只包含一个<code class="docutils literal"><span class="pre">dtype</span></code>。</span><span class="yiyi-st" id="yiyi-462">If you’re unfamiliar with these concepts, you can see <a class="reference internal" href="basics.html#basics-dtypes"><span class="std std-ref">here</span></a> to learn more about dtypes, and <a class="reference internal" href="basics.html#basics-object-conversion"><span class="std std-ref">here</span></a> to learn more about <code class="docutils literal"><span class="pre">object</span></code> conversion in <code class="docutils literal"><span class="pre">pandas</span></code>.</span></p>
<p><span class="yiyi-st" id="yiyi-463">例如，您可以使用<a class="reference internal" href="generated/pandas.read_csv.html#pandas.read_csv" title="pandas.read_csv"><code class="xref py py-func docutils literal"><span class="pre">read_csv()</span></code></a>的<code class="docutils literal"><span class="pre">converters</span></code>参数：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [22]: </span><span class="n">data</span> <span class="o">=</span> <span class="s2">&quot;col_1</span><span class="se">\n</span><span class="s2">1</span><span class="se">\n</span><span class="s2">2</span><span class="se">\n</span><span class="s2">&apos;A&apos;</span><span class="se">\n</span><span class="s2">4.22&quot;</span>

<span class="gp">In [23]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">converters</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;col_1&apos;</span><span class="p">:</span><span class="nb">str</span><span class="p">})</span>

<span class="gp">In [24]: </span><span class="n">df</span>
<span class="gr">Out[24]: </span>
<span class="go">  col_1</span>
<span class="go">0     1</span>
<span class="go">1     2</span>
<span class="go">2   &apos;A&apos;</span>
<span class="go">3  4.22</span>

<span class="gp">In [25]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;col_1&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="nb">type</span><span class="p">)</span><span class="o">.</span><span class="n">value_counts</span><span class="p">()</span>
<span class="gr">Out[25]: </span>
<span class="go">&lt;type &apos;str&apos;&gt;    4</span>
<span class="go">Name: col_1, dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-464">或者您可以使用<a class="reference internal" href="generated/pandas.to_numeric.html#pandas.to_numeric" title="pandas.to_numeric"><code class="xref py py-func docutils literal"><span class="pre">to_numeric()</span></code></a>函数在读取数据后强制dtypes，</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [26]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>

<span class="gp">In [27]: </span><span class="n">df2</span><span class="p">[</span><span class="s1">&apos;col_1&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">to_numeric</span><span class="p">(</span><span class="n">df2</span><span class="p">[</span><span class="s1">&apos;col_1&apos;</span><span class="p">],</span> <span class="n">errors</span><span class="o">=</span><span class="s1">&apos;coerce&apos;</span><span class="p">)</span>

<span class="gp">In [28]: </span><span class="n">df2</span>
<span class="gr">Out[28]: </span>
<span class="go">   col_1</span>
<span class="go">0   1.00</span>
<span class="go">1   2.00</span>
<span class="go">2    NaN</span>
<span class="go">3   4.22</span>

<span class="gp">In [29]: </span><span class="n">df2</span><span class="p">[</span><span class="s1">&apos;col_1&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="nb">type</span><span class="p">)</span><span class="o">.</span><span class="n">value_counts</span><span class="p">()</span>
<span class="gr">Out[29]: </span>
<span class="go">&lt;type &apos;float&apos;&gt;    4</span>
<span class="go">Name: col_1, dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-465">它会将所有有效的解析转换为浮点数，将无效的解析作为<code class="docutils literal"><span class="pre">NaN</span></code>。</span></p>
<p><span class="yiyi-st" id="yiyi-466">最终，如何处理在包含混合dtypes的列中读取取决于您的具体需求。</span><span class="yiyi-st" id="yiyi-467">在上面的情况下，如果你想<code class="docutils literal"><span class="pre">NaN</span></code>输出数据异常，则<a class="reference internal" href="generated/pandas.to_numeric.html#pandas.to_numeric" title="pandas.to_numeric"><code class="xref py py-func docutils literal"><span class="pre">to_numeric()</span></code></a>可能是你最好的选择。</span><span class="yiyi-st" id="yiyi-468">然而，如果你想要强制所有的数据，不管类型，然后使用<a class="reference internal" href="generated/pandas.read_csv.html#pandas.read_csv" title="pandas.read_csv"><code class="xref py py-func docutils literal"><span class="pre">read_csv()</span></code></a>的<code class="docutils literal"><span class="pre">converters</span></code>参数肯定是值得尝试。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-469">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-470"><code class="docutils literal"><span class="pre">dtype</span></code>选项目前仅受C引擎支持。</span><span class="yiyi-st" id="yiyi-471">使用<code class="docutils literal"><span class="pre">engine</span></code>指定<code class="docutils literal"><span class="pre">dtype</span></code>而不是“c”会引发<code class="docutils literal"><span class="pre">ValueError</span></code>。</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-472">注意</span></p>
<p><span class="yiyi-st" id="yiyi-473">在某些情况下，使用包含混合dtypes的列读取异常数据将导致不一致的数据集。</span><span class="yiyi-st" id="yiyi-474">如果你依靠pandas推断你的列的dtypes，解析引擎将去推断不同数据块的dtypes，而不是一次性的整个数据集。</span><span class="yiyi-st" id="yiyi-475">因此，你可以结束与混合dtypes的列。</span><span class="yiyi-st" id="yiyi-476">例如，</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [30]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;col_1&apos;</span><span class="p">:</span><span class="nb">range</span><span class="p">(</span><span class="mi">500000</span><span class="p">)</span> <span class="o">+</span> <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">]</span> <span class="o">+</span> <span class="nb">range</span><span class="p">(</span><span class="mi">500000</span><span class="p">)})</span>

<span class="gp">In [31]: </span><span class="n">df</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="s1">&apos;foo&apos;</span><span class="p">)</span>

<span class="gp">In [32]: </span><span class="n">mixed_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;foo&apos;</span><span class="p">)</span>

<span class="gp">In [33]: </span><span class="n">mixed_df</span><span class="p">[</span><span class="s1">&apos;col_1&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="nb">type</span><span class="p">)</span><span class="o">.</span><span class="n">value_counts</span><span class="p">()</span>
<span class="gr">Out[33]: </span>
<span class="go">&lt;type &apos;int&apos;&gt;    737858</span>
<span class="go">&lt;type &apos;str&apos;&gt;    262144</span>
<span class="go">Name: col_1, dtype: int64</span>

<span class="gp">In [34]: </span><span class="n">mixed_df</span><span class="p">[</span><span class="s1">&apos;col_1&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">dtype</span>
<span class="gr">Out[34]: </span><span class="n">dtype</span><span class="p">(</span><span class="s1">&apos;O&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p class="last"><span class="yiyi-st" id="yiyi-477">将导致<cite>mixed_df</cite>对于某些块的块包含<code class="docutils literal"><span class="pre">int</span></code> dtype，对于其他块包含<code class="docutils literal"><span class="pre">str</span></code>，由于来自数据的混合dty读入。</span><span class="yiyi-st" id="yiyi-478">重要的是要注意，整个列将标记<code class="docutils literal"><span class="pre">object</span></code>的<code class="docutils literal"><span class="pre">dtype</span></code>，用于具有混合dtypes的列。</span></p>
</div>
</div>
<div class="section" id="specifying-categorical-dtype">
<span id="io-categorical"></span><h3><span class="yiyi-st" id="yiyi-479">Specifying Categorical dtype</span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-480"><span class="versionmodified">版本0.19.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-481"><code class="docutils literal"><span class="pre">Categorical</span></code>列可以直接通过指定<code class="docutils literal"><span class="pre">dtype=&apos;category&apos;</span></code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [35]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;col1,col2,col3</span><span class="se">\n</span><span class="s1">a,b,1</span><span class="se">\n</span><span class="s1">a,b,2</span><span class="se">\n</span><span class="s1">c,d,3&apos;</span>

<span class="gp">In [36]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="gr">Out[36]: </span>
<span class="go">  col1 col2  col3</span>
<span class="go">0    a    b     1</span>
<span class="go">1    a    b     2</span>
<span class="go">2    c    d     3</span>

<span class="gp">In [37]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">))</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[37]: </span>
<span class="go">col1    object</span>
<span class="go">col2    object</span>
<span class="go">col3     int64</span>
<span class="go">dtype: object</span>

<span class="gp">In [38]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;category&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[38]: </span>
<span class="go">col1    category</span>
<span class="go">col2    category</span>
<span class="go">col3    category</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-482">可以使用dict规范将各个列解析为<code class="docutils literal"><span class="pre">Categorical</span></code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [39]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;col1&apos;</span><span class="p">:</span> <span class="s1">&apos;category&apos;</span><span class="p">})</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[39]: </span>
<span class="go">col1    category</span>
<span class="go">col2      object</span>
<span class="go">col3       int64</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-483">注意</span></p>
<p><span class="yiyi-st" id="yiyi-484">结果类别将始终被解析为字符串（对象dtype）。</span><span class="yiyi-st" id="yiyi-485">如果类别是数字，则可以使用<a class="reference internal" href="generated/pandas.to_numeric.html#pandas.to_numeric" title="pandas.to_numeric"><code class="xref py py-func docutils literal"><span class="pre">to_numeric()</span></code></a>函数或适当时使用另一个转换器（例如<a class="reference internal" href="generated/pandas.to_datetime.html#pandas.to_datetime" title="pandas.to_datetime"><code class="xref py py-func docutils literal"><span class="pre">to_datetime()</span></code></a>）转换类别。</span></p>
<div class="last highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [40]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;category&apos;</span><span class="p">)</span>

<span class="gp">In [41]: </span><span class="n">df</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[41]: </span>
<span class="go">col1    category</span>
<span class="go">col2    category</span>
<span class="go">col3    category</span>
<span class="go">dtype: object</span>

<span class="gp">In [42]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;col3&apos;</span><span class="p">]</span>
<span class="gr">Out[42]: </span>
<span class="go">0    1</span>
<span class="go">1    2</span>
<span class="go">2    3</span>
<span class="go">Name: col3, dtype: category</span>
<span class="go">Categories (3, object): [1, 2, 3]</span>

<span class="gp">In [43]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;col3&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">cat</span><span class="o">.</span><span class="n">categories</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">to_numeric</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;col3&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">cat</span><span class="o">.</span><span class="n">categories</span><span class="p">)</span>

<span class="gp">In [44]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;col3&apos;</span><span class="p">]</span>
<span class="gr">Out[44]: </span>
<span class="go">0    1</span>
<span class="go">1    2</span>
<span class="go">2    3</span>
<span class="go">Name: col3, dtype: category</span>
<span class="go">Categories (3, int64): [1, 2, 3]</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="naming-and-using-columns">
<h3><span class="yiyi-st" id="yiyi-486">Naming and Using Columns</span></h3>
<div class="section" id="handling-column-names">
<span id="io-headers"></span><h4><span class="yiyi-st" id="yiyi-487">Handling column names</span></h4>
<p><span class="yiyi-st" id="yiyi-488">文件可以有也可以没有标题行。</span><span class="yiyi-st" id="yiyi-489">pandas假设第一行应该用作列名：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [45]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;a,b,c</span><span class="se">\n</span><span class="s1">1,2,3</span><span class="se">\n</span><span class="s1">4,5,6</span><span class="se">\n</span><span class="s1">7,8,9&apos;</span>

<span class="gp">In [46]: </span><span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="go">a,b,c</span>
<span class="go">1,2,3</span>
<span class="go">4,5,6</span>
<span class="go">7,8,9</span>

<span class="gp">In [47]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="gr">Out[47]: </span>
<span class="go">   a  b  c</span>
<span class="go">0  1  2  3</span>
<span class="go">1  4  5  6</span>
<span class="go">2  7  8  9</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-490">通过与<code class="docutils literal"><span class="pre">header</span></code>结合指定<code class="docutils literal"><span class="pre">names</span></code>参数，您可以指示要使用的其他名称以及是否丢弃标题行（如果有）：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [48]: </span><span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="go">a,b,c</span>
<span class="go">1,2,3</span>
<span class="go">4,5,6</span>
<span class="go">7,8,9</span>

<span class="gp">In [49]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">names</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;bar&apos;</span><span class="p">,</span> <span class="s1">&apos;baz&apos;</span><span class="p">],</span> <span class="n">header</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[49]: </span>
<span class="go">   foo  bar  baz</span>
<span class="go">0    1    2    3</span>
<span class="go">1    4    5    6</span>
<span class="go">2    7    8    9</span>

<span class="gp">In [50]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">names</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;bar&apos;</span><span class="p">,</span> <span class="s1">&apos;baz&apos;</span><span class="p">],</span> <span class="n">header</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
<span class="gr">Out[50]: </span>
<span class="go">  foo bar baz</span>
<span class="go">0   a   b   c</span>
<span class="go">1   1   2   3</span>
<span class="go">2   4   5   6</span>
<span class="go">3   7   8   9</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-491">如果标题位于第一行以外的行，请将行号传递到<code class="docutils literal"><span class="pre">header</span></code>。</span><span class="yiyi-st" id="yiyi-492">这将跳过前面的行：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [51]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;skip this skip it</span><span class="se">\n</span><span class="s1">a,b,c</span><span class="se">\n</span><span class="s1">1,2,3</span><span class="se">\n</span><span class="s1">4,5,6</span><span class="se">\n</span><span class="s1">7,8,9&apos;</span>

<span class="gp">In [52]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">header</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[52]: </span>
<span class="go">   a  b  c</span>
<span class="go">0  1  2  3</span>
<span class="go">1  4  5  6</span>
<span class="go">2  7  8  9</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="duplicate-names-parsing">
<span id="io-dupe-names"></span><h3><span class="yiyi-st" id="yiyi-493">Duplicate names parsing</span></h3>
<p><span class="yiyi-st" id="yiyi-494">如果文件或标题包含重复的名称，则pandas默认会对这些名称进行重复数据删除，以防止数据覆盖：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [53]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;a,b,a</span><span class="se">\n</span><span class="s1">0,1,2</span><span class="se">\n</span><span class="s1">3,4,5&apos;</span>

<span class="gp">In [54]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="gr">Out[54]: </span>
<span class="go">   a  b  a.1</span>
<span class="go">0  0  1    2</span>
<span class="go">1  3  4    5</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-495">没有更多的重复数据，因为默认情况下，<code class="docutils literal"><span class="pre">mangle_dupe_cols=True</span></code>会修改一系列重复的列&apos;X&apos;...&apos;X&apos;变成&apos;X.0&apos;...&apos;X.N &apos;。</span><span class="yiyi-st" id="yiyi-496">如果<code class="docutils literal"><span class="pre">mangle_dupe_cols</span> <span class="pre">= False</span></code>，可能会出现重复的数据：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mi">2</span><span class="p">]:</span> <span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;a,b,a</span><span class="se">\n</span><span class="s1">0,1,2</span><span class="se">\n</span><span class="s1">3,4,5&apos;</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">3</span><span class="p">]:</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">mangle_dupe_cols</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">Out</span><span class="p">[</span><span class="mi">3</span><span class="p">]:</span>
   <span class="n">a</span>  <span class="n">b</span>  <span class="n">a</span>
<span class="mi">0</span>  <span class="mi">2</span>  <span class="mi">1</span>  <span class="mi">2</span>
<span class="mi">1</span>  <span class="mi">5</span>  <span class="mi">4</span>  <span class="mi">5</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-497">为了防止用户在重复数据中遇到此问题，如果<code class="docutils literal"><span class="pre">mangle_dupe_cols</span> <span class="pre">！=</span> <span class="pre">True，则会引发<code class="docutils literal"><span class="pre">ValueError</span></code> t5&gt;</span></code>：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mi">2</span><span class="p">]:</span> <span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;a,b,a</span><span class="se">\n</span><span class="s1">0,1,2</span><span class="se">\n</span><span class="s1">3,4,5&apos;</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">3</span><span class="p">]:</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">mangle_dupe_cols</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="o">...</span>
<span class="ne">ValueError</span><span class="p">:</span> <span class="n">Setting</span> <span class="n">mangle_dupe_cols</span><span class="o">=</span><span class="bp">False</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">supported</span> <span class="n">yet</span>
</pre></div>
</div>
<div class="section" id="filtering-columns-usecols">
<span id="io-usecols"></span><h4><span class="yiyi-st" id="yiyi-498">Filtering columns (<code class="docutils literal"><span class="pre">usecols</span></code>)</span></h4>
<p><span class="yiyi-st" id="yiyi-499"><code class="docutils literal"><span class="pre">usecols</span></code>参数允许您使用列名称或位置数字选择文件中的任何列子集：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [55]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;a,b,c,d</span><span class="se">\n</span><span class="s1">1,2,3,foo</span><span class="se">\n</span><span class="s1">4,5,6,bar</span><span class="se">\n</span><span class="s1">7,8,9,baz&apos;</span>

<span class="gp">In [56]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="gr">Out[56]: </span>
<span class="go">   a  b  c    d</span>
<span class="go">0  1  2  3  foo</span>
<span class="go">1  4  5  6  bar</span>
<span class="go">2  7  8  9  baz</span>

<span class="gp">In [57]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">usecols</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">])</span>
<span class="gr">Out[57]: </span>
<span class="go">   b    d</span>
<span class="go">0  2  foo</span>
<span class="go">1  5  bar</span>
<span class="go">2  8  baz</span>

<span class="gp">In [58]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">usecols</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="gr">Out[58]: </span>
<span class="go">   a  c    d</span>
<span class="go">0  1  3  foo</span>
<span class="go">1  4  6  bar</span>
<span class="go">2  7  9  baz</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="comments-and-empty-lines">
<h3><span class="yiyi-st" id="yiyi-500">Comments and Empty Lines</span></h3>
<div class="section" id="ignoring-line-comments-and-empty-lines">
<span id="io-skiplines"></span><h4><span class="yiyi-st" id="yiyi-501">Ignoring line comments and empty lines</span></h4>
<p><span class="yiyi-st" id="yiyi-502">如果指定<code class="docutils literal"><span class="pre">comment</span></code>参数，则完全注释的行将被忽略。</span><span class="yiyi-st" id="yiyi-503">默认情况下，完全空白行也将被忽略。</span><span class="yiyi-st" id="yiyi-504">这两个都是版本0.15中引入的API更改。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [59]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;</span><span class="se">\n</span><span class="s1">a,b,c</span><span class="se">\n</span><span class="s1">  </span><span class="se">\n</span><span class="s1"># commented line</span><span class="se">\n</span><span class="s1">1,2,3</span><span class="se">\n\n</span><span class="s1">4,5,6&apos;</span>

<span class="gp">In [60]: </span><span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

<span class="go">a,b,c</span>
<span class="go">  </span>
<span class="go">1,2,3</span>

<span class="go">4,5,6</span>

<span class="c"># commented line</span>
<span class="gp">In [61]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">comment</span><span class="o">=</span><span class="s1">&apos;#&apos;</span><span class="p">)</span>
<span class="gr">Out[61]: </span>
<span class="go">   a  b  c</span>
<span class="go">0  1  2  3</span>
<span class="go">1  4  5  6</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-505">如果<code class="docutils literal"><span class="pre">skip_blank_lines=False</span></code>，则<code class="docutils literal"><span class="pre">read_csv</span></code>将不会忽略空行：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [62]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;a,b,c</span><span class="se">\n\n</span><span class="s1">1,2,3</span><span class="se">\n\n\n</span><span class="s1">4,5,6&apos;</span>

<span class="gp">In [63]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">skip_blank_lines</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gr">Out[63]: </span>
<span class="go">     a    b    c</span>
<span class="go">0  NaN  NaN  NaN</span>
<span class="go">1  1.0  2.0  3.0</span>
<span class="go">2  NaN  NaN  NaN</span>
<span class="go">3  NaN  NaN  NaN</span>
<span class="go">4  4.0  5.0  6.0</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-506">警告</span></p>
<p><span class="yiyi-st" id="yiyi-507">忽略行的存在可能产生涉及行号的模糊性；参数<code class="docutils literal"><span class="pre">header</span></code>使用行号（忽略注释/空行），而<code class="docutils literal"><span class="pre">skiprows</span></code>使用行号（包括注释/空行）：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [64]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;#comment</span><span class="se">\n</span><span class="s1">a,b,c</span><span class="se">\n</span><span class="s1">A,B,C</span><span class="se">\n</span><span class="s1">1,2,3&apos;</span>

<span class="gp">In [65]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">comment</span><span class="o">=</span><span class="s1">&apos;#&apos;</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[65]: </span>
<span class="go">   A  B  C</span>
<span class="go">0  1  2  3</span>

<span class="gp">In [66]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;A,B,C</span><span class="se">\n</span><span class="s1">#comment</span><span class="se">\n</span><span class="s1">a,b,c</span><span class="se">\n</span><span class="s1">1,2,3&apos;</span>

<span class="gp">In [67]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">comment</span><span class="o">=</span><span class="s1">&apos;#&apos;</span><span class="p">,</span> <span class="n">skiprows</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[67]: </span>
<span class="go">   a  b  c</span>
<span class="go">0  1  2  3</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-508">如果指定<code class="docutils literal"><span class="pre">header</span></code>和<code class="docutils literal"><span class="pre">skiprows</span></code>，则<code class="docutils literal"><span class="pre">header</span></code>将相对于<code class="docutils literal"><span class="pre">skiprows</span></code>的结尾。</span><span class="yiyi-st" id="yiyi-509">例如：</span></p>
<div class="last highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [68]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;# empty</span><span class="se">\n</span><span class="s1"># second empty line</span><span class="se">\n</span><span class="s1"># third empty&apos;</span> \

<span class="gp">In [68]: </span><span class="s1">&apos;line</span><span class="se">\n</span><span class="s1">X,Y,Z</span><span class="se">\n</span><span class="s1">1,2,3</span><span class="se">\n</span><span class="s1">A,B,C</span><span class="se">\n</span><span class="s1">1,2.,4.</span><span class="se">\n</span><span class="s1">5.,NaN,10.0&apos;</span>

<span class="gp">In [69]: </span><span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="c"># empty</span>
<span class="c"># second empty line</span>
<span class="c"># third emptyline</span>
<span class="go">X,Y,Z</span>
<span class="go">1,2,3</span>
<span class="go">A,B,C</span>
<span class="go">1,2.,4.</span>
<span class="go">5.,NaN,10.0</span>

<span class="gp">In [70]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">comment</span><span class="o">=</span><span class="s1">&apos;#&apos;</span><span class="p">,</span> <span class="n">skiprows</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[70]: </span>
<span class="go">     A    B     C</span>
<span class="go">0  1.0  2.0   4.0</span>
<span class="go">1  5.0  NaN  10.0</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="comments">
<span id="io-comments"></span><h4><span class="yiyi-st" id="yiyi-510">Comments</span></h4>
<p><span class="yiyi-st" id="yiyi-511">有时评论或元数据可能包含在文件中：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [71]: </span><span class="k">print</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="go">ID,level,category</span>
<span class="go">Patient1,123000,x # really unpleasant</span>
<span class="go">Patient2,23000,y # wouldn&apos;t take his medicine</span>
<span class="go">Patient3,1234018,z # awesome</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-512">默认情况下，解析器在输出中包括注释：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [72]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">)</span>

<span class="gp">In [73]: </span><span class="n">df</span>
<span class="gr">Out[73]: </span>
<span class="go">         ID    level                        category</span>
<span class="go">0  Patient1   123000           x # really unpleasant</span>
<span class="go">1  Patient2    23000  y # wouldn&apos;t take his medicine</span>
<span class="go">2  Patient3  1234018                     z # awesome</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-513">我们可以使用<code class="docutils literal"><span class="pre">comment</span></code>关键字取消注释：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [74]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">,</span> <span class="n">comment</span><span class="o">=</span><span class="s1">&apos;#&apos;</span><span class="p">)</span>

<span class="gp">In [75]: </span><span class="n">df</span>
<span class="gr">Out[75]: </span>
<span class="go">         ID    level category</span>
<span class="go">0  Patient1   123000       x </span>
<span class="go">1  Patient2    23000       y </span>
<span class="go">2  Patient3  1234018       z </span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="dealing-with-unicode-data">
<span id="io-unicode"></span><h3><span class="yiyi-st" id="yiyi-514">Dealing with Unicode Data</span></h3>
<p><span class="yiyi-st" id="yiyi-515">对于编码的unicode数据，应使用<code class="docutils literal"><span class="pre">encoding</span></code>参数，这将导致在结果中将字节字符串解码为unicode：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [76]: </span><span class="n">data</span> <span class="o">=</span> <span class="n">b</span><span class="s1">&apos;word,length</span><span class="se">\n</span><span class="s1">Tr</span><span class="se">\xc3\xa4</span><span class="s1">umen,7</span><span class="se">\n</span><span class="s1">Gr</span><span class="se">\xc3\xbc\xc3\x9f</span><span class="s1">e,5&apos;</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&apos;utf8&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&apos;latin-1&apos;</span><span class="p">)</span>

<span class="gp">In [77]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">BytesIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">encoding</span><span class="o">=</span><span class="s1">&apos;latin-1&apos;</span><span class="p">)</span>

<span class="gp">In [78]: </span><span class="n">df</span>
<span class="gr">Out[78]: </span>
<span class="go">      word  length</span>
<span class="go">0  Tr&#xE4;umen       7</span>
<span class="go">1    Gr&#xFC;&#xDF;e       5</span>

<span class="gp">In [79]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;word&apos;</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
<span class="gr">Out[79]: </span><span class="s1">u&apos;Gr</span><span class="se">\xfc\xdf</span><span class="s1">e&apos;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-516">一些将所有字符编码为多个字节（如UTF-16）的格式将不会正确解析，而不指定编码。</span><span class="yiyi-st" id="yiyi-517"><a class="reference external" href="https://docs.python.org/3/library/codecs.html#standard-encodings">Python标准编码的完整列表</a></span></p>
</div>
<div class="section" id="index-columns-and-trailing-delimiters">
<span id="io-index-col"></span><h3><span class="yiyi-st" id="yiyi-518">Index columns and trailing delimiters</span></h3>
<p><span class="yiyi-st" id="yiyi-519">如果一个文件还有一列数据而不是列名数，则第一列将被用作DataFrame的行名称：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [80]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;a,b,c</span><span class="se">\n</span><span class="s1">4,apple,bat,5.7</span><span class="se">\n</span><span class="s1">8,orange,cow,10&apos;</span>

<span class="gp">In [81]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="gr">Out[81]: </span>
<span class="go">        a    b     c</span>
<span class="go">4   apple  bat   5.7</span>
<span class="go">8  orange  cow  10.0</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [82]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;index,a,b,c</span><span class="se">\n</span><span class="s1">4,apple,bat,5.7</span><span class="se">\n</span><span class="s1">8,orange,cow,10&apos;</span>

<span class="gp">In [83]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[83]: </span>
<span class="go">            a    b     c</span>
<span class="go">index                   </span>
<span class="go">4       apple  bat   5.7</span>
<span class="go">8      orange  cow  10.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-520">通常，您可以使用<code class="docutils literal"><span class="pre">index_col</span></code>选项来实现此行为。</span></p>
<p><span class="yiyi-st" id="yiyi-521">当在每个数据行的末尾使用定界符准备文件时，会出现一些异常情况，从而导致解析器混乱。</span><span class="yiyi-st" id="yiyi-522">要显式禁用索引列推断并放弃最后一列，请传递<code class="docutils literal"><span class="pre">index_col=False</span></code>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [84]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;a,b,c</span><span class="se">\n</span><span class="s1">4,apple,bat,</span><span class="se">\n</span><span class="s1">8,orange,cow,&apos;</span>

<span class="gp">In [85]: </span><span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="go">a,b,c</span>
<span class="go">4,apple,bat,</span>
<span class="go">8,orange,cow,</span>

<span class="gp">In [86]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="gr">Out[86]: </span>
<span class="go">        a    b   c</span>
<span class="go">4   apple  bat NaN</span>
<span class="go">8  orange  cow NaN</span>

<span class="gp">In [87]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">index_col</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gr">Out[87]: </span>
<span class="go">   a       b    c</span>
<span class="go">0  4   apple  bat</span>
<span class="go">1  8  orange  cow</span>
</pre></div>
</div>
</div>
<div class="section" id="date-handling">
<span id="io-parse-dates"></span><h3><span class="yiyi-st" id="yiyi-523">Date Handling</span></h3>
<div class="section" id="specifying-date-columns">
<h4><span class="yiyi-st" id="yiyi-524">Specifying Date Columns</span></h4>
<p><span class="yiyi-st" id="yiyi-525">为了更好地使用datetime数据，<a class="reference internal" href="generated/pandas.read_csv.html#pandas.read_csv" title="pandas.read_csv"><code class="xref py py-func docutils literal"><span class="pre">read_csv()</span></code></a>和<a class="reference internal" href="generated/pandas.read_table.html#pandas.read_table" title="pandas.read_table"><code class="xref py py-func docutils literal"><span class="pre">read_table()</span></code></a>使用关键字参数<code class="docutils literal"><span class="pre">parse_dates</span></code>和<code class="docutils literal"><span class="pre">date_parser</span></code>以允许用户指定各种列和日期/时间格式将输入文本数据转换为<code class="docutils literal"><span class="pre">datetime</span></code>对象。</span></p>
<p><span class="yiyi-st" id="yiyi-526">最简单的情况是传入<code class="docutils literal"><span class="pre">parse_dates=True</span></code>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># Use a column as an index, and parse it as dates.</span>
<span class="gp">In [88]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;foo.csv&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [89]: </span><span class="n">df</span>
<span class="gr">Out[89]: </span>
<span class="go">            A  B  C</span>
<span class="go">date               </span>
<span class="go">2009-01-01  a  1  2</span>
<span class="go">2009-01-02  b  3  4</span>
<span class="go">2009-01-03  c  4  5</span>

<span class="c"># These are python datetime objects</span>
<span class="gp">In [90]: </span><span class="n">df</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[90]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2009-01-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2009-01-02&apos;</span><span class="p">,</span> <span class="s1">&apos;2009-01-03&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">u&apos;date&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-527">通常情况下，我们可能要分开存储日期和时间数据，或单独存储各种日期字段。</span><span class="yiyi-st" id="yiyi-528"><code class="docutils literal"><span class="pre">parse_dates</span></code>关键字可用于指定解析日期和/或时间的列的组合。</span></p>
<p><span class="yiyi-st" id="yiyi-529">您可以将列列表指定为<code class="docutils literal"><span class="pre">parse_dates</span></code>，生成的日期列将预置到输出（以不影响现有列顺序），新的列名称将是组件列名称：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [91]: </span><span class="k">print</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="go">KORD,19990127, 19:00:00, 18:56:00, 0.8100</span>
<span class="go">KORD,19990127, 20:00:00, 19:56:00, 0.0100</span>
<span class="go">KORD,19990127, 21:00:00, 20:56:00, -0.5900</span>
<span class="go">KORD,19990127, 21:00:00, 21:18:00, -0.9900</span>
<span class="go">KORD,19990127, 22:00:00, 21:56:00, -0.5900</span>
<span class="go">KORD,19990127, 23:00:00, 22:56:00, -0.5900</span>

<span class="gp">In [92]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">]])</span>

<span class="gp">In [93]: </span><span class="n">df</span>
<span class="gr">Out[93]: </span>
<span class="go">                  1_2                 1_3     0     4</span>
<span class="go">0 1999-01-27 19:00:00 1999-01-27 18:56:00  KORD  0.81</span>
<span class="go">1 1999-01-27 20:00:00 1999-01-27 19:56:00  KORD  0.01</span>
<span class="go">2 1999-01-27 21:00:00 1999-01-27 20:56:00  KORD -0.59</span>
<span class="go">3 1999-01-27 21:00:00 1999-01-27 21:18:00  KORD -0.99</span>
<span class="go">4 1999-01-27 22:00:00 1999-01-27 21:56:00  KORD -0.59</span>
<span class="go">5 1999-01-27 23:00:00 1999-01-27 22:56:00  KORD -0.59</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-530">默认情况下，解析器会删除组件日期列，但您可以选择通过<code class="docutils literal"><span class="pre">keep_date_col</span></code>关键字保留它们：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [94]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">]],</span>
<span class="gp">   ....:</span>                  <span class="n">keep_date_col</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">   ....:</span> 

<span class="gp">In [95]: </span><span class="n">df</span>
<span class="gr">Out[95]: </span>
<span class="go">                  1_2                 1_3     0         1          2  \</span>
<span class="go">0 1999-01-27 19:00:00 1999-01-27 18:56:00  KORD  19990127   19:00:00   </span>
<span class="go">1 1999-01-27 20:00:00 1999-01-27 19:56:00  KORD  19990127   20:00:00   </span>
<span class="go">2 1999-01-27 21:00:00 1999-01-27 20:56:00  KORD  19990127   21:00:00   </span>
<span class="go">3 1999-01-27 21:00:00 1999-01-27 21:18:00  KORD  19990127   21:00:00   </span>
<span class="go">4 1999-01-27 22:00:00 1999-01-27 21:56:00  KORD  19990127   22:00:00   </span>
<span class="go">5 1999-01-27 23:00:00 1999-01-27 22:56:00  KORD  19990127   23:00:00   </span>

<span class="go">           3     4  </span>
<span class="go">0   18:56:00  0.81  </span>
<span class="go">1   19:56:00  0.01  </span>
<span class="go">2   20:56:00 -0.59  </span>
<span class="go">3   21:18:00 -0.99  </span>
<span class="go">4   21:56:00 -0.59  </span>
<span class="go">5   22:56:00 -0.59  </span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-531">请注意，如果您希望将多个列合并到单个日期列中，则必须使用嵌套列表。</span><span class="yiyi-st" id="yiyi-532">In other words, <code class="docutils literal"><span class="pre">parse_dates=[1,</span> <span class="pre">2]</span></code> indicates that the second and third columns should each be parsed as separate date columns while <code class="docutils literal"><span class="pre">parse_dates=[[1,</span> <span class="pre">2]]</span></code> means the two columns should be parsed into a single column.</span></p>
<p><span class="yiyi-st" id="yiyi-533">您还可以使用dict指定自定义名称列：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [96]: </span><span class="n">date_spec</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&apos;nominal&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="s1">&apos;actual&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">]}</span>

<span class="gp">In [97]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="n">date_spec</span><span class="p">)</span>

<span class="gp">In [98]: </span><span class="n">df</span>
<span class="gr">Out[98]: </span>
<span class="go">              nominal              actual     0     4</span>
<span class="go">0 1999-01-27 19:00:00 1999-01-27 18:56:00  KORD  0.81</span>
<span class="go">1 1999-01-27 20:00:00 1999-01-27 19:56:00  KORD  0.01</span>
<span class="go">2 1999-01-27 21:00:00 1999-01-27 20:56:00  KORD -0.59</span>
<span class="go">3 1999-01-27 21:00:00 1999-01-27 21:18:00  KORD -0.99</span>
<span class="go">4 1999-01-27 22:00:00 1999-01-27 21:56:00  KORD -0.59</span>
<span class="go">5 1999-01-27 23:00:00 1999-01-27 22:56:00  KORD -0.59</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-534">重要的是要记住，如果要将多个文本列解析为单个日期列，则会在数据前面添加一个新列。</span><span class="yiyi-st" id="yiyi-535"><cite>index_col</cite>规范基于此新的列集合，而不是原始数据列：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [99]: </span><span class="n">date_spec</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&apos;nominal&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="s1">&apos;actual&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">]}</span>

<span class="gp">In [100]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="n">date_spec</span><span class="p">,</span>
<span class="gp">   .....:</span>                  <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="c1">#index is the nominal column</span>
<span class="gp">   .....:</span> 

<span class="gp">In [101]: </span><span class="n">df</span>
<span class="gr">Out[101]: </span>
<span class="go">                                 actual     0     4</span>
<span class="go">nominal                                            </span>
<span class="go">1999-01-27 19:00:00 1999-01-27 18:56:00  KORD  0.81</span>
<span class="go">1999-01-27 20:00:00 1999-01-27 19:56:00  KORD  0.01</span>
<span class="go">1999-01-27 21:00:00 1999-01-27 20:56:00  KORD -0.59</span>
<span class="go">1999-01-27 21:00:00 1999-01-27 21:18:00  KORD -0.99</span>
<span class="go">1999-01-27 22:00:00 1999-01-27 21:56:00  KORD -0.59</span>
<span class="go">1999-01-27 23:00:00 1999-01-27 22:56:00  KORD -0.59</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-536">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-537">read_csv有一个fast_path用于解析iso8601格式的日期时间字符串，例如“2000-01-01T00：01：02 + 00：00”和类似的变体。</span><span class="yiyi-st" id="yiyi-538">如果您可以安排您的数据以此格式存储数据时间，加载时间将显着更快，观察到约20倍。</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-539">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-540">当传递一个dict作为<cite>parse_dates</cite>参数时，不保证前置列的顺序，因为<cite>dict</cite>对象不对它们的键施加排序。</span><span class="yiyi-st" id="yiyi-541">在Python 2.7+上，如果这对你很重要，你可以使用<cite>collections.OrderedDict</cite>而不是普通的<cite>dict</cite>。</span><span class="yiyi-st" id="yiyi-542">因此，当对于&apos;parse_dates&apos;与<cite>index_col</cite>参数结合使用dict时，最好将<cite>index_col</cite>指定为列标签，而不是作为结果的索引帧。</span></p>
</div>
</div>
<div class="section" id="date-parsing-functions">
<h4><span class="yiyi-st" id="yiyi-543">Date Parsing Functions</span></h4>
<p><span class="yiyi-st" id="yiyi-544">最后，解析器允许您指定自定义<code class="docutils literal"><span class="pre">date_parser</span></code>函数，以充分利用日期解析API的灵活性：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [102]: </span><span class="kn">import</span> <span class="nn">pandas.io.date_converters</span> <span class="kn">as</span> <span class="nn">conv</span>

<span class="gp">In [103]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="n">date_spec</span><span class="p">,</span>
<span class="gp">   .....:</span>                  <span class="n">date_parser</span><span class="o">=</span><span class="n">conv</span><span class="o">.</span><span class="n">parse_date_time</span><span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [104]: </span><span class="n">df</span>
<span class="gr">Out[104]: </span>
<span class="go">              nominal              actual     0     4</span>
<span class="go">0 1999-01-27 19:00:00 1999-01-27 18:56:00  KORD  0.81</span>
<span class="go">1 1999-01-27 20:00:00 1999-01-27 19:56:00  KORD  0.01</span>
<span class="go">2 1999-01-27 21:00:00 1999-01-27 20:56:00  KORD -0.59</span>
<span class="go">3 1999-01-27 21:00:00 1999-01-27 21:18:00  KORD -0.99</span>
<span class="go">4 1999-01-27 22:00:00 1999-01-27 21:56:00  KORD -0.59</span>
<span class="go">5 1999-01-27 23:00:00 1999-01-27 22:56:00  KORD -0.59</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-545">Pandas将尝试以三种不同的方式调用<code class="docutils literal"><span class="pre">date_parser</span></code>函数。</span><span class="yiyi-st" id="yiyi-546">如果引发异常，则尝试下一个异常：</span></p>
<ol class="arabic simple">
<li><span class="yiyi-st" id="yiyi-547">首先使用<cite>parse_dates</cite>（例如，<code class="docutils literal"><span class="pre">date_parser（[&apos;2013&apos;，</span>]）定义一个或多个数组作为参数调用<code class="docutils literal"><span class="pre">date_parser</span></code> <span class="pre">&apos;2013&apos;]，</span> <span class="pre">[&apos;1&apos;，</span> <span class="pre">&apos;2&apos;]）</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-548">如果＃1失败，则调用<code class="docutils literal"><span class="pre">date_parser</span></code>，所有列按行连接到单个数组中（例如，<code class="docutils literal"><span class="pre">date_parser（[&apos;2013</span> <span class="pre">1&apos; ，</span> <span class="pre">&apos;2013</span> <span class="pre">2&apos;]）</span></code>）</span></li>
<li><span class="yiyi-st" id="yiyi-549">如果＃2失败，则对于具有来自<cite>parse_dates</cite>指示的列中的一个或多个字符串参数的每一行调用<code class="docutils literal"><span class="pre">date_parser</span></code>一次（例如，<code class="docutils literal"><span class="pre">date_parser 2013“，” <span class="pre">&apos;2&apos;）的第一行（<code class="docutils literal"><span class="pre">2013&apos;，</span></code></span></span> <span class="pre">&apos;1&apos;）</span> 用于第二个，等等）</code></span></li>
</ol>
<p><span class="yiyi-st" id="yiyi-550">注意，在性能方面，你应该尝试这些方法按顺序解析日期：</span></p>
<ol class="arabic simple">
<li><span class="yiyi-st" id="yiyi-551">尝试使用<code class="docutils literal"><span class="pre">infer_datetime_format=True</span></code>（请参见下面部分）推断格式</span></li>
<li><span class="yiyi-st" id="yiyi-552">如果您知道格式，请使用<code class="docutils literal"><span class="pre">pd.to_datetime()</span></code>：<code class="docutils literal"><span class="pre">date_parser = lambda</span> <span class="pre">x：</span> <span class="pre">pd.to_datetime ，</span> <span class="pre">format = ...）</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-553">如果您有非标准格式，请使用自定义<code class="docutils literal"><span class="pre">date_parser</span></code>函数。</span><span class="yiyi-st" id="yiyi-554">为了获得最佳性能，这应该是向量化的，即它应该接受数组作为参数。</span></li>
</ol>
<p><span class="yiyi-st" id="yiyi-555">您可以在<code class="docutils literal"><span class="pre">date_converters.py</span></code>中探索日期解析功能，并添加自己的日期解析功能。</span><span class="yiyi-st" id="yiyi-556">我们希望将这个模块变成一个社区支持的日期/时间解析器集合。</span><span class="yiyi-st" id="yiyi-557">为了让您入门，<code class="docutils literal"><span class="pre">date_converters.py</span></code>包含用于解析双日期和时间列，年/月/日列和年/月/日/小时/分/秒列的功能。</span><span class="yiyi-st" id="yiyi-558">它还包含一个<code class="docutils literal"><span class="pre">generic_parser</span></code>函数，因此您可以使用处理单个日期而不是整个数组的函数来对其进行curry。</span></p>
</div>
<div class="section" id="inferring-datetime-format">
<span id="io-dayfirst"></span><h4><span class="yiyi-st" id="yiyi-559">Inferring Datetime Format</span></h4>
<p><span class="yiyi-st" id="yiyi-560">如果您为某些或所有列启用了<code class="docutils literal"><span class="pre">parse_dates</span></code>，并且datetime字符串都采用相同的格式，则可以通过设置<code class="docutils literal"><span class="pre">infer_datetime_format=True</span></code> 。</span><span class="yiyi-st" id="yiyi-561">如果设置，pandas将尝试猜测datetime字符串的格式，然后使用更快的方式解析字符串。</span><span class="yiyi-st" id="yiyi-562">观察到5-10x解析速度。</span><span class="yiyi-st" id="yiyi-563">pandas将回退到通常的解析，如果格式不能猜到或者猜测的格式不能正确解析整个字符串列。</span><span class="yiyi-st" id="yiyi-564">因此，一般来说，如果启用<code class="docutils literal"><span class="pre">infer_datetime_format</span></code>，则不应产生任何负面结果。</span></p>
<p><span class="yiyi-st" id="yiyi-565">以下是可以猜测的日期时间字符串的一些示例（全部表示2011年12月30日00:00:00）</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-566">“20111230”</span></li>
<li><span class="yiyi-st" id="yiyi-567">“2011/12/30”</span></li>
<li><span class="yiyi-st" id="yiyi-568">“20111230 00:00:00”</span></li>
<li><span class="yiyi-st" id="yiyi-569">“12/30/2011 00:00:00”</span></li>
<li><span class="yiyi-st" id="yiyi-570">“30 / Dec / 2011 00:00:00”</span></li>
<li><span class="yiyi-st" id="yiyi-571">“30 / December / 2011 00:00:00”</span></li>
</ul>
<p><span class="yiyi-st" id="yiyi-572"><code class="docutils literal"><span class="pre">infer_datetime_format</span></code>对<code class="docutils literal"><span class="pre">dayfirst</span></code>敏感。</span><span class="yiyi-st" id="yiyi-573">使用<code class="docutils literal"><span class="pre">dayfirst=True</span></code>，它会猜到“01/12/2011”为12月1日。</span><span class="yiyi-st" id="yiyi-574">使用<code class="docutils literal"><span class="pre">dayfirst=False</span></code>（默认），它会猜到“01/12/2011”为1月12日。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># Try to infer the format for the index column</span>
<span class="gp">In [105]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;foo.csv&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="gp">   .....:</span>                  <span class="n">infer_datetime_format</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [106]: </span><span class="n">df</span>
<span class="gr">Out[106]: </span>
<span class="go">            A  B  C</span>
<span class="go">date               </span>
<span class="go">2009-01-01  a  1  2</span>
<span class="go">2009-01-02  b  3  4</span>
<span class="go">2009-01-03  c  4  5</span>
</pre></div>
</div>
</div>
<div class="section" id="international-date-formats">
<h4><span class="yiyi-st" id="yiyi-575">International Date Formats</span></h4>
<p><span class="yiyi-st" id="yiyi-576">虽然美国日期格式通常为MM / DD / YYYY，但许多国际格式使用DD / MM / YYYY。</span><span class="yiyi-st" id="yiyi-577">为方便起见，提供了<code class="docutils literal"><span class="pre">dayfirst</span></code>关键字：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [107]: </span><span class="k">print</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="go">date,value,cat</span>
<span class="go">1/6/2000,5,a</span>
<span class="go">2/6/2000,10,b</span>
<span class="go">3/6/2000,15,c</span>

<span class="gp">In [108]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="gr">Out[108]: </span>
<span class="go">        date  value cat</span>
<span class="go">0 2000-01-06      5   a</span>
<span class="go">1 2000-02-06     10   b</span>
<span class="go">2 2000-03-06     15   c</span>

<span class="gp">In [109]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">,</span> <span class="n">dayfirst</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="gr">Out[109]: </span>
<span class="go">        date  value cat</span>
<span class="go">0 2000-06-01      5   a</span>
<span class="go">1 2000-06-02     10   b</span>
<span class="go">2 2000-06-03     15   c</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="specifying-method-for-floating-point-conversion">
<span id="io-float-precision"></span><h3><span class="yiyi-st" id="yiyi-578">Specifying method for floating-point conversion</span></h3>
<p><span class="yiyi-st" id="yiyi-579">可以指定参数<code class="docutils literal"><span class="pre">float_precision</span></code>，以便在使用C引擎进行解析期间使用特定的浮点转换器。</span><span class="yiyi-st" id="yiyi-580">选项是普通转换器，高精度转换器和往返转换器（在写入文件后保证往返值）。</span><span class="yiyi-st" id="yiyi-581">例如：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [110]: </span><span class="n">val</span> <span class="o">=</span> <span class="s1">&apos;0.3066101993807095471566981359501369297504425048828125&apos;</span>

<span class="gp">In [111]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;a,b,c</span><span class="se">\n</span><span class="s1">1,2,{0}&apos;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>

<span class="gp">In [112]: </span><span class="nb">abs</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">engine</span><span class="o">=</span><span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="n">float_precision</span><span class="o">=</span><span class="bp">None</span><span class="p">)[</span><span class="s1">&apos;c&apos;</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="nb">float</span><span class="p">(</span><span class="n">val</span><span class="p">))</span>
<span class="gr">Out[112]: </span><span class="mf">1.1102230246251565e-16</span>

<span class="gp">In [113]: </span><span class="nb">abs</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">engine</span><span class="o">=</span><span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="n">float_precision</span><span class="o">=</span><span class="s1">&apos;high&apos;</span><span class="p">)[</span><span class="s1">&apos;c&apos;</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="nb">float</span><span class="p">(</span><span class="n">val</span><span class="p">))</span>
<span class="gr">Out[113]: </span><span class="mf">5.5511151231257827e-17</span>

<span class="gp">In [114]: </span><span class="nb">abs</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">engine</span><span class="o">=</span><span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="n">float_precision</span><span class="o">=</span><span class="s1">&apos;round_trip&apos;</span><span class="p">)[</span><span class="s1">&apos;c&apos;</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="nb">float</span><span class="p">(</span><span class="n">val</span><span class="p">))</span>
<span class="gr">Out[114]: </span><span class="mf">0.0</span>
</pre></div>
</div>
</div>
<div class="section" id="thousand-separators">
<span id="io-thousands"></span><h3><span class="yiyi-st" id="yiyi-582">Thousand Separators</span></h3>
<p><span class="yiyi-st" id="yiyi-583">对于已使用千位分隔符写入的大数字，可以将<code class="docutils literal"><span class="pre">thousands</span></code>关键字设置为长度为1的字符串，以便整数将被正确解析：</span></p>
<p><span class="yiyi-st" id="yiyi-584">默认情况下，带有千位分隔符的数字将被解析为字符串</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [115]: </span><span class="k">print</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="go">ID|level|category</span>
<span class="go">Patient1|123,000|x</span>
<span class="go">Patient2|23,000|y</span>
<span class="go">Patient3|1,234,018|z</span>

<span class="gp">In [116]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&apos;|&apos;</span><span class="p">)</span>

<span class="gp">In [117]: </span><span class="n">df</span>
<span class="gr">Out[117]: </span>
<span class="go">         ID      level category</span>
<span class="go">0  Patient1    123,000        x</span>
<span class="go">1  Patient2     23,000        y</span>
<span class="go">2  Patient3  1,234,018        z</span>

<span class="gp">In [118]: </span><span class="n">df</span><span class="o">.</span><span class="n">level</span><span class="o">.</span><span class="n">dtype</span>
<span class="gr">Out[118]: </span><span class="n">dtype</span><span class="p">(</span><span class="s1">&apos;O&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-585"><code class="docutils literal"><span class="pre">thousands</span></code>关键字允许正确解析整数</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [119]: </span><span class="k">print</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="go">ID|level|category</span>
<span class="go">Patient1|123,000|x</span>
<span class="go">Patient2|23,000|y</span>
<span class="go">Patient3|1,234,018|z</span>

<span class="gp">In [120]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&apos;|&apos;</span><span class="p">,</span> <span class="n">thousands</span><span class="o">=</span><span class="s1">&apos;,&apos;</span><span class="p">)</span>

<span class="gp">In [121]: </span><span class="n">df</span>
<span class="gr">Out[121]: </span>
<span class="go">         ID    level category</span>
<span class="go">0  Patient1   123000        x</span>
<span class="go">1  Patient2    23000        y</span>
<span class="go">2  Patient3  1234018        z</span>

<span class="gp">In [122]: </span><span class="n">df</span><span class="o">.</span><span class="n">level</span><span class="o">.</span><span class="n">dtype</span>
<span class="gr">Out[122]: </span><span class="n">dtype</span><span class="p">(</span><span class="s1">&apos;int64&apos;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="na-values">
<span id="io-na-values"></span><h3><span class="yiyi-st" id="yiyi-586">NA Values</span></h3>
<p><span class="yiyi-st" id="yiyi-587">要控制哪些值被解析为缺失值（由<code class="docutils literal"><span class="pre">NaN</span></code>表示），请在<code class="docutils literal"><span class="pre">na_values</span></code>中指定一个字符串。</span><span class="yiyi-st" id="yiyi-588">如果指定字符串列表，则其中的所有值都将被视为缺少值。</span><span class="yiyi-st" id="yiyi-589">如果您指定一个数字（<code class="docutils literal"><span class="pre">float</span></code>，例如<code class="docutils literal"><span class="pre">5.0</span></code>或<code class="docutils literal"><span class="pre">integer</span></code>，例如<code class="docutils literal"><span class="pre">5</span></code>），也将意味着缺失值（在这种情况下，有效地<code class="docutils literal"><span class="pre">[5.0,5]</span></code>被识别为<code class="docutils literal"><span class="pre">NaN</span></code>。</span></p>
<p><span class="yiyi-st" id="yiyi-590">要完全覆盖被识别为缺少的默认值，请指定<code class="docutils literal"><span class="pre">keep_default_na=False</span></code>。</span><span class="yiyi-st" id="yiyi-591">The default <code class="docutils literal"><span class="pre">NaN</span></code> recognized values are <code class="docutils literal"><span class="pre">[&apos;-1.#IND&apos;,</span> <span class="pre">&apos;1.#QNAN&apos;,</span> <span class="pre">&apos;1.#IND&apos;,</span> <span class="pre">&apos;-1.#QNAN&apos;,</span> <span class="pre">&apos;#N/A&apos;,&apos;N/A&apos;,</span> <span class="pre">&apos;NA&apos;,</span> <span class="pre">&apos;#NA&apos;,</span> <span class="pre">&apos;NULL&apos;,</span> <span class="pre">&apos;NaN&apos;,</span> <span class="pre">&apos;-NaN&apos;,</span> <span class="pre">&apos;nan&apos;,</span> <span class="pre">&apos;-nan&apos;]</span></code>. </span><span class="yiyi-st" id="yiyi-592">虽然长度为0的字符串<code class="docutils literal"><span class="pre">&apos;&apos;</span></code>不包含在默认的<code class="docutils literal"><span class="pre">NaN</span></code>值列表中，但仍被视为缺失值。</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">read_csv</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">na_values</span><span class="o">=</span><span class="p">[</span><span class="mi">5</span><span class="p">])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-593"><code class="docutils literal"><span class="pre">5</span></code>，<code class="docutils literal"><span class="pre">5.0</span></code>被解释为数字被识别为<code class="docutils literal"><span class="pre">NaN</span></code></span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">read_csv</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">keep_default_na</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">na_values</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;&quot;</span><span class="p">])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-594">只有空字段为<code class="docutils literal"><span class="pre">NaN</span></code></span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">read_csv</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">keep_default_na</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">na_values</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;NA&quot;</span><span class="p">,</span> <span class="s2">&quot;0&quot;</span><span class="p">])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-595">只有<code class="docutils literal"><span class="pre">NA</span></code>和<code class="docutils literal"><span class="pre">0</span></code>作为字符串是<code class="docutils literal"><span class="pre">NaN</span></code></span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">read_csv</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">na_values</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;Nope&quot;</span><span class="p">])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-596">字符串<code class="docutils literal"><span class="pre">&quot;Nope&quot;</span></code>被识别为<code class="docutils literal"><span class="pre">NaN</span></code></span></p>
</div>
<div class="section" id="infinity">
<span id="io-infinity"></span><h3><span class="yiyi-st" id="yiyi-597">Infinity</span></h3>
<p><span class="yiyi-st" id="yiyi-598"><code class="docutils literal"><span class="pre">inf</span></code>的值将被解析为<code class="docutils literal"><span class="pre">np.inf</span></code>（正无穷大），<code class="docutils literal"><span class="pre">-inf</span></code>为<code class="docutils literal"><span class="pre">-np.inf</span></code></span><span class="yiyi-st" id="yiyi-599">这些将忽略该值的情况，意思是<code class="docutils literal"><span class="pre">Inf</span></code>，也将被解析为<code class="docutils literal"><span class="pre">np.inf</span></code>。</span></p>
</div>
<div class="section" id="returning-series">
<h3><span class="yiyi-st" id="yiyi-600">Returning Series</span></h3>
<p><span class="yiyi-st" id="yiyi-601">使用<code class="docutils literal"><span class="pre">squeeze</span></code>关键字，解析器将返回单个列作为<code class="docutils literal"><span class="pre">Series</span></code>的输出：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [123]: </span><span class="k">print</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="go">level</span>
<span class="go">Patient1,123000</span>
<span class="go">Patient2,23000</span>
<span class="go">Patient3,1234018</span>

<span class="gp">In [124]: </span><span class="n">output</span> <span class="o">=</span>  <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;tmp.csv&apos;</span><span class="p">,</span> <span class="n">squeeze</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [125]: </span><span class="n">output</span>
<span class="gr">Out[125]: </span>
<span class="go">Patient1     123000</span>
<span class="go">Patient2      23000</span>
<span class="go">Patient3    1234018</span>
<span class="go">Name: level, dtype: int64</span>

<span class="gp">In [126]: </span><span class="nb">type</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="gr">Out[126]: </span><span class="n">pandas</span><span class="o">.</span><span class="n">core</span><span class="o">.</span><span class="n">series</span><span class="o">.</span><span class="n">Series</span>
</pre></div>
</div>
</div>
<div class="section" id="boolean-values">
<span id="io-boolean"></span><h3><span class="yiyi-st" id="yiyi-602">Boolean values</span></h3>
<p><span class="yiyi-st" id="yiyi-603">公共值<code class="docutils literal"><span class="pre">True</span></code>，<code class="docutils literal"><span class="pre">False</span></code>，<code class="docutils literal"><span class="pre">TRUE</span></code>和<code class="docutils literal"><span class="pre">FALSE</span></code>都被识别为布尔值。</span><span class="yiyi-st" id="yiyi-604">有时你想要识别一些其他值为布尔。</span><span class="yiyi-st" id="yiyi-605">为此，请使用<code class="docutils literal"><span class="pre">true_values</span></code>和<code class="docutils literal"><span class="pre">false_values</span></code>选项：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [127]: </span><span class="n">data</span><span class="o">=</span> <span class="s1">&apos;a,b,c</span><span class="se">\n</span><span class="s1">1,Yes,2</span><span class="se">\n</span><span class="s1">3,No,4&apos;</span>

<span class="gp">In [128]: </span><span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="go">a,b,c</span>
<span class="go">1,Yes,2</span>
<span class="go">3,No,4</span>

<span class="gp">In [129]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="gr">Out[129]: </span>
<span class="go">   a    b  c</span>
<span class="go">0  1  Yes  2</span>
<span class="go">1  3   No  4</span>

<span class="gp">In [130]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">true_values</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;Yes&apos;</span><span class="p">],</span> <span class="n">false_values</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;No&apos;</span><span class="p">])</span>
<span class="gr">Out[130]: </span>
<span class="go">   a      b  c</span>
<span class="go">0  1   True  2</span>
<span class="go">1  3  False  4</span>
</pre></div>
</div>
</div>
<div class="section" id="handling-bad-lines">
<span id="io-bad-lines"></span><h3><span class="yiyi-st" id="yiyi-606">Handling “bad” lines</span></h3>
<p><span class="yiyi-st" id="yiyi-607">某些文件可能包含字段太少或太多的格式不正确的行。</span><span class="yiyi-st" id="yiyi-608">具有太少字段的行将在尾部字段中填充NA值。</span><span class="yiyi-st" id="yiyi-609">过多的行会默认产生错误：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [27]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;a,b,c</span><span class="se">\n</span><span class="s1">1,2,3</span><span class="se">\n</span><span class="s1">4,5,6,7</span><span class="se">\n</span><span class="s1">8,9,10&apos;</span>

<span class="gp">In [28]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="go">---------------------------------------------------------------------------</span>
<span class="go">CParserError                              Traceback (most recent call last)</span>
<span class="go">CParserError: Error tokenizing data. C error: Expected 3 fields in line 3, saw 4</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-610">你可以选择跳过坏行：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [29]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">error_bad_lines</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="go">Skipping line 3: expected 3 fields, saw 4</span>

<span class="go">Out[29]:</span>
<span class="go">   a  b   c</span>
<span class="go">0  1  2   3</span>
<span class="go">1  8  9  10</span>
</pre></div>
</div>
</div>
<div class="section" id="quoting-and-escape-characters">
<span id="io-quoting"></span><h3><span class="yiyi-st" id="yiyi-611">Quoting and Escape Characters</span></h3>
<p><span class="yiyi-st" id="yiyi-612">嵌入字段中的引号（和其他转义字符）可以以任何数量的方式处理。</span><span class="yiyi-st" id="yiyi-613">一种方法是使用反斜杠；要正确解析此数据，您应该传递<code class="docutils literal"><span class="pre">escapechar</span></code>选项：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [131]: </span><span class="n">data</span> <span class="o">=</span> <span class="s1">&apos;a,b</span><span class="se">\n</span><span class="s1">&quot;hello, </span><span class="se">\\</span><span class="s1">&quot;Bob</span><span class="se">\\</span><span class="s1">&quot;, nice to see you&quot;,5&apos;</span>

<span class="gp">In [132]: </span><span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="go">a,b</span>
<span class="go">&quot;hello, \&quot;Bob\&quot;, nice to see you&quot;,5</span>

<span class="gp">In [133]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">escapechar</span><span class="o">=</span><span class="s1">&apos;</span><span class="se">\\</span><span class="s1">&apos;</span><span class="p">)</span>
<span class="gr">Out[133]: </span>
<span class="go">                               a  b</span>
<span class="go">0  hello, &quot;Bob&quot;, nice to see you  5</span>
</pre></div>
</div>
</div>
<div class="section" id="files-with-fixed-width-columns">
<span id="io-fwf"></span><h3><span class="yiyi-st" id="yiyi-614">Files with Fixed Width Columns</span></h3>
<p><span class="yiyi-st" id="yiyi-615">当<code class="docutils literal"><span class="pre">read_csv</span></code>读取定界数据时，<a class="reference internal" href="generated/pandas.read_fwf.html#pandas.read_fwf" title="pandas.read_fwf"><code class="xref py py-func docutils literal"><span class="pre">read_fwf()</span></code></a>函数适用于已知和固定列宽的数据文件。</span><span class="yiyi-st" id="yiyi-616"><code class="docutils literal"><span class="pre">read_fwf</span></code>的函数参数与具有两个额外参数的<cite>read_csv</cite>大致相同：</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-617"><code class="docutils literal"><span class="pre">colspecs</span></code>：将每行的固定宽度字段的范围作为半开间隔（即，从[from，to]）的对（元组）列表。</span><span class="yiyi-st" id="yiyi-618">字符串值“infer”可以用于指示解析器尝试从数据的前100行中检测列规范。</span><span class="yiyi-st" id="yiyi-619">默认行为，如果没有指定，是推断。</span></li>
<li><span class="yiyi-st" id="yiyi-620"><code class="docutils literal"><span class="pre">widths</span></code>：如果间隔是连续的，可以使用字段宽度列表来代替“colspecs”。</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-621">考虑一个典型的固定宽度数据文件：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [134]: </span><span class="k">print</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;bar.csv&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="go">id8141    360.242940   149.910199   11950.7</span>
<span class="go">id1594    444.953632   166.985655   11788.4</span>
<span class="go">id1849    364.136849   183.628767   11806.2</span>
<span class="go">id1230    413.836124   184.375703   11916.8</span>
<span class="go">id1948    502.953953   173.237159   12468.3</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-622">为了将该文件解析为DataFrame，我们只需要向<cite>read_fwf</cite>函数提供列规范以及文件名：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c">#Column specifications are a list of half-intervals</span>
<span class="gp">In [135]: </span><span class="n">colspecs</span> <span class="o">=</span> <span class="p">[(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">6</span><span class="p">),</span> <span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="mi">20</span><span class="p">),</span> <span class="p">(</span><span class="mi">21</span><span class="p">,</span> <span class="mi">33</span><span class="p">),</span> <span class="p">(</span><span class="mi">34</span><span class="p">,</span> <span class="mi">43</span><span class="p">)]</span>

<span class="gp">In [136]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_fwf</span><span class="p">(</span><span class="s1">&apos;bar.csv&apos;</span><span class="p">,</span> <span class="n">colspecs</span><span class="o">=</span><span class="n">colspecs</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

<span class="gp">In [137]: </span><span class="n">df</span>
<span class="gr">Out[137]: </span>
<span class="go">                 1           2        3</span>
<span class="go">0                                      </span>
<span class="go">id8141  360.242940  149.910199  11950.7</span>
<span class="go">id1594  444.953632  166.985655  11788.4</span>
<span class="go">id1849  364.136849  183.628767  11806.2</span>
<span class="go">id1230  413.836124  184.375703  11916.8</span>
<span class="go">id1948  502.953953  173.237159  12468.3</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-623">注意解析器如何自动选择列名X.当指定<code class="docutils literal"><span class="pre">header=None</span></code>参数时，<column number="">。 </column></span><span class="yiyi-st" id="yiyi-624">或者，您可以只提供连续列的列宽：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c">#Widths are a list of integers</span>
<span class="gp">In [138]: </span><span class="n">widths</span> <span class="o">=</span> <span class="p">[</span><span class="mi">6</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">10</span><span class="p">]</span>

<span class="gp">In [139]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_fwf</span><span class="p">(</span><span class="s1">&apos;bar.csv&apos;</span><span class="p">,</span> <span class="n">widths</span><span class="o">=</span><span class="n">widths</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>

<span class="gp">In [140]: </span><span class="n">df</span>
<span class="gr">Out[140]: </span>
<span class="go">        0           1           2        3</span>
<span class="go">0  id8141  360.242940  149.910199  11950.7</span>
<span class="go">1  id1594  444.953632  166.985655  11788.4</span>
<span class="go">2  id1849  364.136849  183.628767  11806.2</span>
<span class="go">3  id1230  413.836124  184.375703  11916.8</span>
<span class="go">4  id1948  502.953953  173.237159  12468.3</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-625">解析器将处理列周围的额外的空白空间，因此在文件中的列之间可以有额外的分隔。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-626"><span class="versionmodified">版本0.13.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-627">默认情况下，<code class="docutils literal"><span class="pre">read_fwf</span></code>会尝试使用文件的前100行来推断文件的<code class="docutils literal"><span class="pre">colspecs</span></code>。</span><span class="yiyi-st" id="yiyi-628">它只能在列对齐并通过提供的<code class="docutils literal"><span class="pre">delimiter</span></code>（默认分隔符为空格）正确分隔的情况下才能执行。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [141]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_fwf</span><span class="p">(</span><span class="s1">&apos;bar.csv&apos;</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

<span class="gp">In [142]: </span><span class="n">df</span>
<span class="gr">Out[142]: </span>
<span class="go">                 1           2        3</span>
<span class="go">0                                      </span>
<span class="go">id8141  360.242940  149.910199  11950.7</span>
<span class="go">id1594  444.953632  166.985655  11788.4</span>
<span class="go">id1849  364.136849  183.628767  11806.2</span>
<span class="go">id1230  413.836124  184.375703  11916.8</span>
<span class="go">id1948  502.953953  173.237159  12468.3</span>
</pre></div>
</div>
</div>
<div class="section" id="indexes">
<h3><span class="yiyi-st" id="yiyi-629">Indexes</span></h3>
<div class="section" id="files-with-an-implicit-index-column">
<h4><span class="yiyi-st" id="yiyi-630">Files with an “implicit” index column</span></h4>
<p><span class="yiyi-st" id="yiyi-631">考虑一个文件在头中少于一个数据列数：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [143]: </span><span class="k">print</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;foo.csv&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="go">A,B,C</span>
<span class="go">20090101,a,1,2</span>
<span class="go">20090102,b,3,4</span>
<span class="go">20090103,c,4,5</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-632">在这种特殊情况下，<code class="docutils literal"><span class="pre">read_csv</span></code>假定第一列将用作DataFrame的索引：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [144]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;foo.csv&apos;</span><span class="p">)</span>
<span class="gr">Out[144]: </span>
<span class="go">          A  B  C</span>
<span class="go">20090101  a  1  2</span>
<span class="go">20090102  b  3  4</span>
<span class="go">20090103  c  4  5</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-633">请注意，日期不会自动解析。</span><span class="yiyi-st" id="yiyi-634">在这种情况下，你需要像以前一样做：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [145]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;foo.csv&apos;</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [146]: </span><span class="n">df</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[146]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2009-01-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2009-01-02&apos;</span><span class="p">,</span> <span class="s1">&apos;2009-01-03&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="reading-an-index-with-a-multiindex">
<h4><span class="yiyi-st" id="yiyi-635">Reading an index with a <code class="docutils literal"><span class="pre">MultiIndex</span></code></span></h4>
<p id="io-csv-multiindex"><span class="yiyi-st" id="yiyi-636">假设您的数据由两列索引：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [147]: </span><span class="k">print</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;data/mindex_ex.csv&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="go">year,indiv,zit,xit</span>
<span class="go">1977,&quot;A&quot;,1.2,.6</span>
<span class="go">1977,&quot;B&quot;,1.5,.5</span>
<span class="go">1977,&quot;C&quot;,1.7,.8</span>
<span class="go">1978,&quot;A&quot;,.2,.06</span>
<span class="go">1978,&quot;B&quot;,.7,.2</span>
<span class="go">1978,&quot;C&quot;,.8,.3</span>
<span class="go">1978,&quot;D&quot;,.9,.5</span>
<span class="go">1978,&quot;E&quot;,1.4,.9</span>
<span class="go">1979,&quot;C&quot;,.2,.15</span>
<span class="go">1979,&quot;D&quot;,.14,.05</span>
<span class="go">1979,&quot;E&quot;,.5,.15</span>
<span class="go">1979,&quot;F&quot;,1.2,.5</span>
<span class="go">1979,&quot;G&quot;,3.4,1.9</span>
<span class="go">1979,&quot;H&quot;,5.4,2.7</span>
<span class="go">1979,&quot;I&quot;,6.4,1.2</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-637"><code class="docutils literal"><span class="pre">read_csv</span></code>和<code class="docutils literal"><span class="pre">read_table</span></code>的<code class="docutils literal"><span class="pre">index_col</span></code>参数可以使用列号列表将多个列转换为<code class="docutils literal"><span class="pre">MultiIndex</span></code>返回对象的索引：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [148]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s2">&quot;data/mindex_ex.csv&quot;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">])</span>

<span class="gp">In [149]: </span><span class="n">df</span>
<span class="gr">Out[149]: </span>
<span class="go">             zit   xit</span>
<span class="go">year indiv            </span>
<span class="go">1977 A      1.20  0.60</span>
<span class="go">     B      1.50  0.50</span>
<span class="go">     C      1.70  0.80</span>
<span class="go">1978 A      0.20  0.06</span>
<span class="go">     B      0.70  0.20</span>
<span class="go">     C      0.80  0.30</span>
<span class="go">     D      0.90  0.50</span>
<span class="go">     E      1.40  0.90</span>
<span class="go">1979 C      0.20  0.15</span>
<span class="go">     D      0.14  0.05</span>
<span class="go">     E      0.50  0.15</span>
<span class="go">     F      1.20  0.50</span>
<span class="go">     G      3.40  1.90</span>
<span class="go">     H      5.40  2.70</span>
<span class="go">     I      6.40  1.20</span>

<span class="gp">In [150]: </span><span class="n">df</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">1978</span><span class="p">]</span>
<span class="gr">Out[150]: </span>
<span class="go">       zit   xit</span>
<span class="go">indiv           </span>
<span class="go">A      0.2  0.06</span>
<span class="go">B      0.7  0.20</span>
<span class="go">C      0.8  0.30</span>
<span class="go">D      0.9  0.50</span>
<span class="go">E      1.4  0.90</span>
</pre></div>
</div>
</div>
<div class="section" id="reading-columns-with-a-multiindex">
<span id="io-multi-index-columns"></span><h4><span class="yiyi-st" id="yiyi-638">Reading columns with a <code class="docutils literal"><span class="pre">MultiIndex</span></code></span></h4>
<p><span class="yiyi-st" id="yiyi-639">通过指定<code class="docutils literal"><span class="pre">header</span></code>参数的行位置列表，您可以在<code class="docutils literal"><span class="pre">MultiIndex</span></code>中读取列。</span><span class="yiyi-st" id="yiyi-640">指定非连续行将跳过中间行。</span><span class="yiyi-st" id="yiyi-641">为了具有tingleizing列的pre-0.13行为，请指定<code class="docutils literal"><span class="pre">tupleize_cols=True</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [151]: </span><span class="kn">from</span> <span class="nn">pandas.util.testing</span> <span class="kn">import</span> <span class="n">makeCustomDataframe</span> <span class="k">as</span> <span class="n">mkdf</span>

<span class="gp">In [152]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">mkdf</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="n">r_idx_nlevels</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span><span class="n">c_idx_nlevels</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>

<span class="gp">In [153]: </span><span class="n">df</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="s1">&apos;mi.csv&apos;</span><span class="p">)</span>

<span class="gp">In [154]: </span><span class="k">print</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;mi.csv&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="go">C0,,C_l0_g0,C_l0_g1,C_l0_g2</span>
<span class="go">C1,,C_l1_g0,C_l1_g1,C_l1_g2</span>
<span class="go">C2,,C_l2_g0,C_l2_g1,C_l2_g2</span>
<span class="go">C3,,C_l3_g0,C_l3_g1,C_l3_g2</span>
<span class="go">R0,R1,,,</span>
<span class="go">R_l0_g0,R_l1_g0,R0C0,R0C1,R0C2</span>
<span class="go">R_l0_g1,R_l1_g1,R1C0,R1C1,R1C2</span>
<span class="go">R_l0_g2,R_l1_g2,R2C0,R2C1,R2C2</span>
<span class="go">R_l0_g3,R_l1_g3,R3C0,R3C1,R3C2</span>
<span class="go">R_l0_g4,R_l1_g4,R4C0,R4C1,R4C2</span>


<span class="gp">In [155]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;mi.csv&apos;</span><span class="p">,</span><span class="n">header</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span><span class="n">index_col</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">])</span>
<span class="gr">Out[155]: </span>
<span class="go">C0              C_l0_g0 C_l0_g1 C_l0_g2</span>
<span class="go">C1              C_l1_g0 C_l1_g1 C_l1_g2</span>
<span class="go">C2              C_l2_g0 C_l2_g1 C_l2_g2</span>
<span class="go">C3              C_l3_g0 C_l3_g1 C_l3_g2</span>
<span class="go">R0      R1                             </span>
<span class="go">R_l0_g0 R_l1_g0    R0C0    R0C1    R0C2</span>
<span class="go">R_l0_g1 R_l1_g1    R1C0    R1C1    R1C2</span>
<span class="go">R_l0_g2 R_l1_g2    R2C0    R2C1    R2C2</span>
<span class="go">R_l0_g3 R_l1_g3    R3C0    R3C1    R3C2</span>
<span class="go">R_l0_g4 R_l1_g4    R4C0    R4C1    R4C2</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-642">从0.13.0开始，<code class="docutils literal"><span class="pre">read_csv</span></code>将能够解释更常见的多列索引格式。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [156]: </span><span class="k">print</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;mi2.csv&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="go">,a,a,a,b,c,c</span>
<span class="go">,q,r,s,t,u,v</span>
<span class="go">one,1,2,3,4,5,6</span>
<span class="go">two,7,8,9,10,11,12</span>

<span class="gp">In [157]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;mi2.csv&apos;</span><span class="p">,</span><span class="n">header</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">],</span><span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[157]: </span>
<span class="go">     a         b   c    </span>
<span class="go">     q  r  s   t   u   v</span>
<span class="go">one  1  2  3   4   5   6</span>
<span class="go">two  7  8  9  10  11  12</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-643">Note: If an <code class="docutils literal"><span class="pre">index_col</span></code> is not specified (e.g. you don’t have an index, or wrote it with <code class="docutils literal"><span class="pre">df.to_csv(...,</span> <span class="pre">index=False</span></code>), then any <code class="docutils literal"><span class="pre">names</span></code> on the columns index will be <em>lost</em>.</span></p>
</div>
</div>
<div class="section" id="automatically-sniffing-the-delimiter">
<span id="io-sniff"></span><h3><span class="yiyi-st" id="yiyi-644">Automatically “sniffing” the delimiter</span></h3>
<p><span class="yiyi-st" id="yiyi-645"><code class="docutils literal"><span class="pre">read_csv</span></code>能够推断定界（不一定是逗号分隔）文件，因为pandas使用csv模块的<a class="reference external" href="https://docs.python.org/3/library/csv.html#csv.Sniffer" title="(in Python v3.6)"><code class="docutils literal"><span class="pre">csv.Sniffer</span></code></a>类。</span><span class="yiyi-st" id="yiyi-646">为此，您必须指定<code class="docutils literal"><span class="pre">sep=None</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [158]: </span><span class="k">print</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;tmp2.sv&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="go">:0:1:2:3</span>
<span class="go">0:0.469112299907:-0.282863344329:-1.50905850317:-1.13563237102</span>
<span class="go">1:1.21211202502:-0.173214649053:0.119208711297:-1.04423596628</span>
<span class="go">2:-0.861848963348:-2.10456921889:-0.494929274069:1.07180380704</span>
<span class="go">3:0.721555162244:-0.70677113363:-1.03957498511:0.271859885543</span>
<span class="go">4:-0.424972329789:0.567020349794:0.276232019278:-1.08740069129</span>
<span class="go">5:-0.673689708088:0.113648409689:-1.47842655244:0.524987667115</span>
<span class="go">6:0.40470521868:0.57704598592:-1.71500201611:-1.03926848351</span>
<span class="go">7:-0.370646858236:-1.15789225064:-1.34431181273:0.844885141425</span>
<span class="go">8:1.07576978372:-0.10904997528:1.64356307036:-1.46938795954</span>
<span class="go">9:0.357020564133:-0.67460010373:-1.77690371697:-0.968913812447</span>


<span class="gp">In [159]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;tmp2.sv&apos;</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">engine</span><span class="o">=</span><span class="s1">&apos;python&apos;</span><span class="p">)</span>
<span class="gr">Out[159]: </span>
<span class="go">   Unnamed: 0         0         1         2         3</span>
<span class="go">0           0  0.469112 -0.282863 -1.509059 -1.135632</span>
<span class="go">1           1  1.212112 -0.173215  0.119209 -1.044236</span>
<span class="go">2           2 -0.861849 -2.104569 -0.494929  1.071804</span>
<span class="go">3           3  0.721555 -0.706771 -1.039575  0.271860</span>
<span class="go">4           4 -0.424972  0.567020  0.276232 -1.087401</span>
<span class="go">5           5 -0.673690  0.113648 -1.478427  0.524988</span>
<span class="go">6           6  0.404705  0.577046 -1.715002 -1.039268</span>
<span class="go">7           7 -0.370647 -1.157892 -1.344312  0.844885</span>
<span class="go">8           8  1.075770 -0.109050  1.643563 -1.469388</span>
<span class="go">9           9  0.357021 -0.674600 -1.776904 -0.968914</span>
</pre></div>
</div>
</div>
<div class="section" id="iterating-through-files-chunk-by-chunk">
<span id="io-chunking"></span><h3><span class="yiyi-st" id="yiyi-647">Iterating through files chunk by chunk</span></h3>
<p><span class="yiyi-st" id="yiyi-648">假设您希望延迟遍历一个（可能非常大）的文件，而不是将整个文件读入内存，如下所示：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [160]: </span><span class="k">print</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;tmp.sv&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="go">|0|1|2|3</span>
<span class="go">0|0.469112299907|-0.282863344329|-1.50905850317|-1.13563237102</span>
<span class="go">1|1.21211202502|-0.173214649053|0.119208711297|-1.04423596628</span>
<span class="go">2|-0.861848963348|-2.10456921889|-0.494929274069|1.07180380704</span>
<span class="go">3|0.721555162244|-0.70677113363|-1.03957498511|0.271859885543</span>
<span class="go">4|-0.424972329789|0.567020349794|0.276232019278|-1.08740069129</span>
<span class="go">5|-0.673689708088|0.113648409689|-1.47842655244|0.524987667115</span>
<span class="go">6|0.40470521868|0.57704598592|-1.71500201611|-1.03926848351</span>
<span class="go">7|-0.370646858236|-1.15789225064|-1.34431181273|0.844885141425</span>
<span class="go">8|1.07576978372|-0.10904997528|1.64356307036|-1.46938795954</span>
<span class="go">9|0.357020564133|-0.67460010373|-1.77690371697|-0.968913812447</span>


<span class="gp">In [161]: </span><span class="n">table</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_table</span><span class="p">(</span><span class="s1">&apos;tmp.sv&apos;</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&apos;|&apos;</span><span class="p">)</span>

<span class="gp">In [162]: </span><span class="n">table</span>
<span class="gr">Out[162]: </span>
<span class="go">   Unnamed: 0         0         1         2         3</span>
<span class="go">0           0  0.469112 -0.282863 -1.509059 -1.135632</span>
<span class="go">1           1  1.212112 -0.173215  0.119209 -1.044236</span>
<span class="go">2           2 -0.861849 -2.104569 -0.494929  1.071804</span>
<span class="go">3           3  0.721555 -0.706771 -1.039575  0.271860</span>
<span class="go">4           4 -0.424972  0.567020  0.276232 -1.087401</span>
<span class="go">5           5 -0.673690  0.113648 -1.478427  0.524988</span>
<span class="go">6           6  0.404705  0.577046 -1.715002 -1.039268</span>
<span class="go">7           7 -0.370647 -1.157892 -1.344312  0.844885</span>
<span class="go">8           8  1.075770 -0.109050  1.643563 -1.469388</span>
<span class="go">9           9  0.357021 -0.674600 -1.776904 -0.968914</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-649">通过指定<code class="docutils literal"><span class="pre">chunksize</span></code>到<code class="docutils literal"><span class="pre">read_csv</span></code>或<code class="docutils literal"><span class="pre">read_table</span></code>，返回值将是<code class="docutils literal"><span class="pre">TextFileReader</span></code>类型的可迭代对象：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [163]: </span><span class="n">reader</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_table</span><span class="p">(</span><span class="s1">&apos;tmp.sv&apos;</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&apos;|&apos;</span><span class="p">,</span> <span class="n">chunksize</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>

<span class="gp">In [164]: </span><span class="n">reader</span>
<span class="gr">Out[164]: </span><span class="o">&lt;</span><span class="n">pandas</span><span class="o">.</span><span class="n">io</span><span class="o">.</span><span class="n">parsers</span><span class="o">.</span><span class="n">TextFileReader</span> <span class="n">at</span> <span class="mh">0x7ff27e15a450</span><span class="o">&gt;</span>

<span class="gp">In [165]: </span><span class="k">for</span> <span class="n">chunk</span> <span class="ow">in</span> <span class="n">reader</span><span class="p">:</span>
<span class="gp">   .....:</span>     <span class="k">print</span><span class="p">(</span><span class="n">chunk</span><span class="p">)</span>
<span class="gp">   .....:</span> 
<span class="go">   Unnamed: 0         0         1         2         3</span>
<span class="go">0           0  0.469112 -0.282863 -1.509059 -1.135632</span>
<span class="go">1           1  1.212112 -0.173215  0.119209 -1.044236</span>
<span class="go">2           2 -0.861849 -2.104569 -0.494929  1.071804</span>
<span class="go">3           3  0.721555 -0.706771 -1.039575  0.271860</span>
<span class="go">   Unnamed: 0         0         1         2         3</span>
<span class="go">4           4 -0.424972  0.567020  0.276232 -1.087401</span>
<span class="go">5           5 -0.673690  0.113648 -1.478427  0.524988</span>
<span class="go">6           6  0.404705  0.577046 -1.715002 -1.039268</span>
<span class="go">7           7 -0.370647 -1.157892 -1.344312  0.844885</span>
<span class="go">   Unnamed: 0         0        1         2         3</span>
<span class="go">8           8  1.075770 -0.10905  1.643563 -1.469388</span>
<span class="go">9           9  0.357021 -0.67460 -1.776904 -0.968914</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-650">指定<code class="docutils literal"><span class="pre">iterator=True</span></code>也会返回<code class="docutils literal"><span class="pre">TextFileReader</span></code>对象：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [166]: </span><span class="n">reader</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_table</span><span class="p">(</span><span class="s1">&apos;tmp.sv&apos;</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&apos;|&apos;</span><span class="p">,</span> <span class="n">iterator</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [167]: </span><span class="n">reader</span><span class="o">.</span><span class="n">get_chunk</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="gr">Out[167]: </span>
<span class="go">   Unnamed: 0         0         1         2         3</span>
<span class="go">0           0  0.469112 -0.282863 -1.509059 -1.135632</span>
<span class="go">1           1  1.212112 -0.173215  0.119209 -1.044236</span>
<span class="go">2           2 -0.861849 -2.104569 -0.494929  1.071804</span>
<span class="go">3           3  0.721555 -0.706771 -1.039575  0.271860</span>
<span class="go">4           4 -0.424972  0.567020  0.276232 -1.087401</span>
</pre></div>
</div>
</div>
<div class="section" id="specifying-the-parser-engine">
<h3><span class="yiyi-st" id="yiyi-651">Specifying the parser engine</span></h3>
<p><span class="yiyi-st" id="yiyi-652">引擎下pandas使用一个快速和高效的解析器实现在C以及一个python实现，目前更功能完整。</span><span class="yiyi-st" id="yiyi-653">在可能的情况下，pandas使用C解析器（指定为<code class="docutils literal"><span class="pre">engine=&apos;c&apos;</span></code>），但如果指定了C不支持的选项，则可能会回到python。</span><span class="yiyi-st" id="yiyi-654">目前，C不受支持的选项包括：</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-655"><code class="docutils literal"><span class="pre">sep</span></code>而不是单个字符（例如，regex分隔符）</span></li>
<li><span class="yiyi-st" id="yiyi-656"><code class="docutils literal"><span class="pre">skipfooter</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-657"><code class="docutils literal"><span class="pre">sep=None</span></code>与<code class="docutils literal"><span class="pre">delim_whitespace=False</span></code></span></li>
</ul>
<p><span class="yiyi-st" id="yiyi-658">指定以上任何选项将产生<code class="docutils literal"><span class="pre">ParserWarning</span></code>，除非使用<code class="docutils literal"><span class="pre">engine=&apos;python&apos;</span></code>明确选择了python引擎。</span></p>
</div>
<div class="section" id="writing-out-data">
<h3><span class="yiyi-st" id="yiyi-659">Writing out Data</span></h3>
<div class="section" id="writing-to-csv-format">
<span id="io-store-in-csv"></span><h4><span class="yiyi-st" id="yiyi-660">Writing to CSV format</span></h4>
<p><span class="yiyi-st" id="yiyi-661">Series和DataFrame对象具有一个实例方法<code class="docutils literal"><span class="pre">to_csv</span></code>，它允许将对象的内容存储为逗号分隔值文件。</span><span class="yiyi-st" id="yiyi-662">该函数接受一些参数。</span><span class="yiyi-st" id="yiyi-663">只有第一个是必需的。</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-664"><code class="docutils literal"><span class="pre">path_or_buf</span></code>：要写入的文件或StringIO的字符串路径</span></li>
<li><span class="yiyi-st" id="yiyi-665"><code class="docutils literal"><span class="pre">sep</span></code>：输出文件的字段分隔符（默认为“，”）</span></li>
<li><span class="yiyi-st" id="yiyi-666"><code class="docutils literal"><span class="pre">na_rep</span></code>：缺少值的字符串表示形式（默认为&apos;&apos;）</span></li>
<li><span class="yiyi-st" id="yiyi-667"><code class="docutils literal"><span class="pre">float_format</span></code>：格式化浮点数字符串</span></li>
<li><span class="yiyi-st" id="yiyi-668"><code class="docutils literal"><span class="pre">cols</span></code>：要写入的列（默认值为无）</span></li>
<li><span class="yiyi-st" id="yiyi-669"><code class="docutils literal"><span class="pre">header</span></code>：是否写出列名（默认为True）</span></li>
<li><span class="yiyi-st" id="yiyi-670"><code class="docutils literal"><span class="pre">index</span></code>：是否写入行（索引）名称（默认为True）</span></li>
<li><span class="yiyi-st" id="yiyi-671"><code class="docutils literal"><span class="pre">index_label</span></code>：索引列的列标签（如果需要）。</span><span class="yiyi-st" id="yiyi-672">如果无（默认），<cite>头</cite>和<cite>index</cite>为True，则使用索引名称。</span><span class="yiyi-st" id="yiyi-673">（如果DataFrame使用MultiIndex，应该给出一个序列）。</span></li>
<li><span class="yiyi-st" id="yiyi-674"><code class="docutils literal"><span class="pre">mode</span></code>：Python写模式，默认为&apos;w&apos;</span></li>
<li><span class="yiyi-st" id="yiyi-675"><code class="docutils literal"><span class="pre">encoding</span></code>：表示要使用的编码的字符串，如果内容是非ASCII的，对于python版本3之前</span></li>
<li><span class="yiyi-st" id="yiyi-676"><code class="docutils literal"><span class="pre">line_terminator</span></code>：表示行结束的字符序列（默认&apos;\ n&apos;）</span></li>
<li><span class="yiyi-st" id="yiyi-677"><code class="docutils literal"><span class="pre">quoting</span></code>：在csv模块中设置引用规则（默认为csv.QUOTE_MINIMAL）。</span><span class="yiyi-st" id="yiyi-678">注意，如果你设置了一个<cite>float_format</cite>，那么浮点数被转换为字符串，csv.QUOTE_NONNUMERIC会将它们作为非数字</span></li>
<li><span class="yiyi-st" id="yiyi-679"><code class="docutils literal"><span class="pre">quotechar</span></code>：用于引用字段的字符（默认&apos;“&apos;）</span></li>
<li><span class="yiyi-st" id="yiyi-680"><code class="docutils literal"><span class="pre">doublequote</span></code>：在字段中控制<code class="docutils literal"><span class="pre">quotechar</span></code>的引用（默认值为True）</span></li>
<li><span class="yiyi-st" id="yiyi-681"><code class="docutils literal"><span class="pre">escapechar</span></code>：适当时用于转义<code class="docutils literal"><span class="pre">sep</span></code>和<code class="docutils literal"><span class="pre">quotechar</span></code>的字符（默认值为None）</span></li>
<li><span class="yiyi-st" id="yiyi-682"><code class="docutils literal"><span class="pre">chunksize</span></code>：每次写入的行数</span></li>
<li><span class="yiyi-st" id="yiyi-683"><code class="docutils literal"><span class="pre">tupleize_cols</span></code>：如果为False（默认值），则写为元组列表，否则以适用于<code class="docutils literal"><span class="pre">read_csv</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-684"><code class="docutils literal"><span class="pre">date_format</span></code>：格式化datetime对象的字符串</span></li>
</ul>
</div></blockquote>
</div>
<div class="section" id="writing-a-formatted-string">
<h4><span class="yiyi-st" id="yiyi-685">Writing a formatted string</span></h4>
<p id="io-formatting"><span class="yiyi-st" id="yiyi-686">DataFrame对象有一个实例方法<code class="docutils literal"><span class="pre">to_string</span></code>，它允许控制对象的字符串表示。</span><span class="yiyi-st" id="yiyi-687">所有参数都是可选的：</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-688"><code class="docutils literal"><span class="pre">buf</span></code>默认值无，例如StringIO对象</span></li>
<li><span class="yiyi-st" id="yiyi-689"><code class="docutils literal"><span class="pre">columns</span></code>默认值无，要写入的列</span></li>
<li><span class="yiyi-st" id="yiyi-690"><code class="docutils literal"><span class="pre">col_space</span></code>默认值无，每列的最小宽度。</span></li>
<li><span class="yiyi-st" id="yiyi-691"><code class="docutils literal"><span class="pre">na_rep</span></code>默认<code class="docutils literal"><span class="pre">NaN</span></code>，表示NA值</span></li>
<li><span class="yiyi-st" id="yiyi-692"><code class="docutils literal"><span class="pre">formatters</span></code> default无，函数的字典（按列），每个函数接受单个参数并返回格式化的字符串</span></li>
<li><span class="yiyi-st" id="yiyi-693"><code class="docutils literal"><span class="pre">float_format</span></code> default无，一个函数，它接受一个（float）参数并返回一个格式化的字符串；以应用于DataFrame中的浮动。</span></li>
<li><span class="yiyi-st" id="yiyi-694"><code class="docutils literal"><span class="pre">sparsify</span></code> default True，对于具有层次索引的DataFrame，设置为False以在每行打印每个多索引键。</span></li>
<li><span class="yiyi-st" id="yiyi-695"><code class="docutils literal"><span class="pre">index_names</span></code>默认为True，将打印索引的名称</span></li>
<li><span class="yiyi-st" id="yiyi-696"><code class="docutils literal"><span class="pre">index</span></code> default True，将打印索引（即行标签）</span></li>
<li><span class="yiyi-st" id="yiyi-697"><code class="docutils literal"><span class="pre">header</span></code> default True，将打印列标签</span></li>
<li><span class="yiyi-st" id="yiyi-698"><code class="docutils literal"><span class="pre">justify</span></code>默认<code class="docutils literal"><span class="pre">left</span></code>，将左对齐或右对齐打印列标题</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-699">Series对象还有一个<code class="docutils literal"><span class="pre">to_string</span></code>方法，但只有<code class="docutils literal"><span class="pre">buf</span></code>，<code class="docutils literal"><span class="pre">na_rep</span></code>，<code class="docutils literal"><span class="pre">float_format</span></code>参数。</span><span class="yiyi-st" id="yiyi-700">还有一个<code class="docutils literal"><span class="pre">length</span></code>参数，如果设置为<code class="docutils literal"><span class="pre">True</span></code>，将另外输出系列的长度。</span></p>
</div>
</div>
</div>
<div class="section" id="json">
<span id="io-json"></span><h2><span class="yiyi-st" id="yiyi-701">JSON</span></h2>
<p><span class="yiyi-st" id="yiyi-702">读取和写入<code class="docutils literal"><span class="pre">JSON</span></code>格式的文件和字符串。</span></p>
<div class="section" id="writing-json">
<span id="io-json-writer"></span><h3><span class="yiyi-st" id="yiyi-703">Writing JSON</span></h3>
<p><span class="yiyi-st" id="yiyi-704">可以将<code class="docutils literal"><span class="pre">Series</span></code>或<code class="docutils literal"><span class="pre">DataFrame</span></code>转换为有效的JSON字符串。</span><span class="yiyi-st" id="yiyi-705">使用可选参数<code class="docutils literal"><span class="pre">to_json</span></code>：</span></p>
<ul>
<li><p class="first"><span class="yiyi-st" id="yiyi-706"><code class="docutils literal"><span class="pre">path_or_buf</span></code>：写入输出的路径名或缓冲区可以是<code class="docutils literal"><span class="pre">None</span></code>，在这种情况下返回JSON字符串</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-707"><code class="docutils literal"><span class="pre">orient</span></code>：</span></p>
<dl class="docutils">
<dt><span class="yiyi-st" id="yiyi-708">系列：</span></dt>
<dd><ul class="first last simple">
<li><span class="yiyi-st" id="yiyi-709">默认为<code class="docutils literal"><span class="pre">index</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-710">允许值为{<code class="docutils literal"><span class="pre">split</span></code>，<code class="docutils literal"><span class="pre">records</span></code>，<code class="docutils literal"><span class="pre">index</span></code>}</span></li>
</ul>
</dd>
<dt><span class="yiyi-st" id="yiyi-711">DataFrame</span></dt>
<dd><ul class="first last simple">
<li><span class="yiyi-st" id="yiyi-712">默认为<code class="docutils literal"><span class="pre">columns</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-713">允许值为{<code class="docutils literal"><span class="pre">split</span></code>，<code class="docutils literal"><span class="pre">records</span></code>，<code class="docutils literal"><span class="pre">index</span></code>，<code class="docutils literal"><span class="pre">columns</span></code>，<code class="docutils literal"><span class="pre">values</span></code></span></li>
</ul>
</dd>
</dl>
<p><span class="yiyi-st" id="yiyi-714">JSON字符串的格式</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="12%">
<col width="88%">
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-715"><code class="docutils literal"><span class="pre">split</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-716">dict like {index  - &gt; [index]，columns  - &gt; [columns]，data  - &gt; [values]}</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-717"><code class="docutils literal"><span class="pre">records</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-718">列表像[{column  - &gt; value}，...，{column  - &gt; value}]</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-719"><code class="docutils literal"><span class="pre">index</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-720">dict like {index  - &gt; {column  - &gt; value}}</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-721"><code class="docutils literal"><span class="pre">columns</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-722">dict like {column  - &gt; {index  - &gt; value}}</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-723"><code class="docutils literal"><span class="pre">values</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-724">只是值数组</span></td>
</tr>
</tbody>
</table>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-725"><code class="docutils literal"><span class="pre">date_format</span></code>：字符串，日期转换类型，“epoch”表示时间戳，“iso”表示ISO8601。</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-726"><code class="docutils literal"><span class="pre">double_precision</span></code>：编码浮点值时使用的小数位数，默认值为10。</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-727"><code class="docutils literal"><span class="pre">force_ascii</span></code>：强制编码字符串为ASCII，默认为True。</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-728"><code class="docutils literal"><span class="pre">date_unit</span></code>：要编码的时间单位，支配时间戳和ISO8601精度。</span><span class="yiyi-st" id="yiyi-729">分别为秒，毫秒，微秒和纳秒的&apos;s&apos;，&apos;ms&apos;，&apos;us&apos;或&apos;ns&apos;之一。</span><span class="yiyi-st" id="yiyi-730">默认值“ms”。</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-731"><code class="docutils literal"><span class="pre">default_handler</span></code>：如果对象无法转换为适当的JSON格式，则调用的处理程序。</span><span class="yiyi-st" id="yiyi-732">采用单个参数，它是要转换的对象，并返回可序列化对象。</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-733"><code class="docutils literal"><span class="pre">lines</span></code>：如果<code class="docutils literal"><span class="pre">records</span></code> orient，则将每行的每条记录作为json写入。</span></p>
</li>
</ul>
<p><span class="yiyi-st" id="yiyi-734">Note <code class="docutils literal"><span class="pre">NaN</span></code>‘s, <code class="docutils literal"><span class="pre">NaT</span></code>‘s and <code class="docutils literal"><span class="pre">None</span></code> will be converted to <code class="docutils literal"><span class="pre">null</span></code> and <code class="docutils literal"><span class="pre">datetime</span></code> objects will be converted based on the <code class="docutils literal"><span class="pre">date_format</span></code> and <code class="docutils literal"><span class="pre">date_unit</span></code> parameters.</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [168]: </span><span class="n">dfj</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;AB&apos;</span><span class="p">))</span>

<span class="gp">In [169]: </span><span class="n">json</span> <span class="o">=</span> <span class="n">dfj</span><span class="o">.</span><span class="n">to_json</span><span class="p">()</span>

<span class="gp">In [170]: </span><span class="n">json</span>
<span class="gr">Out[170]: </span><span class="s1">&apos;{&quot;A&quot;:{&quot;0&quot;:-1.2945235903,&quot;1&quot;:0.2766617129,&quot;2&quot;:-0.0139597524,&quot;3&quot;:-0.0061535699,&quot;4&quot;:0.8957173022},&quot;B&quot;:{&quot;0&quot;:0.4137381054,&quot;1&quot;:-0.472034511,&quot;2&quot;:-0.3625429925,&quot;3&quot;:-0.923060654,&quot;4&quot;:0.8052440254}}&apos;</span>
</pre></div>
</div>
<div class="section" id="orient-options">
<h4><span class="yiyi-st" id="yiyi-735">Orient Options</span></h4>
<p><span class="yiyi-st" id="yiyi-736">生成的JSON文件/字符串的格式有多种不同的选项。</span><span class="yiyi-st" id="yiyi-737">考虑下面的DataFrame和Series：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [171]: </span><span class="n">dfjo</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">A</span><span class="o">=</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="n">B</span><span class="o">=</span><span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">7</span><span class="p">),</span> <span class="n">C</span><span class="o">=</span><span class="nb">range</span><span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">10</span><span class="p">)),</span>
<span class="gp">   .....:</span>                     <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;ABC&apos;</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;xyz&apos;</span><span class="p">))</span>
<span class="gp">   .....:</span> 

<span class="gp">In [172]: </span><span class="n">dfjo</span>
<span class="gr">Out[172]: </span>
<span class="go">   A  B  C</span>
<span class="go">x  1  4  7</span>
<span class="go">y  2  5  8</span>
<span class="go">z  3  6  9</span>

<span class="gp">In [173]: </span><span class="n">sjo</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">15</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">16</span><span class="p">,</span> <span class="n">z</span><span class="o">=</span><span class="mi">17</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="gp">In [174]: </span><span class="n">sjo</span>
<span class="gr">Out[174]: </span>
<span class="go">x    15</span>
<span class="go">y    16</span>
<span class="go">z    17</span>
<span class="go">Name: D, dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-738"><strong>面向列</strong>（<code class="docutils literal"><span class="pre">DataFrame</span></code>的默认值）将数据序列化为以列标签作为主索引的嵌套JSON对象：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [175]: </span><span class="n">dfjo</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">orient</span><span class="o">=</span><span class="s2">&quot;columns&quot;</span><span class="p">)</span>
<span class="gr">Out[175]: </span><span class="s1">&apos;{&quot;A&quot;:{&quot;x&quot;:1,&quot;y&quot;:2,&quot;z&quot;:3},&quot;B&quot;:{&quot;x&quot;:4,&quot;y&quot;:5,&quot;z&quot;:6},&quot;C&quot;:{&quot;x&quot;:7,&quot;y&quot;:8,&quot;z&quot;:9}}&apos;</span>

</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-739"><strong>面向索引</strong>（类似于<code class="docutils literal"><span class="pre">Series</span></code>的默认值）类似于面向列，但索引标签现在是主要的：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [176]: </span><span class="n">dfjo</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">orient</span><span class="o">=</span><span class="s2">&quot;index&quot;</span><span class="p">)</span>
<span class="gr">Out[176]: </span><span class="s1">&apos;{&quot;x&quot;:{&quot;A&quot;:1,&quot;B&quot;:4,&quot;C&quot;:7},&quot;y&quot;:{&quot;A&quot;:2,&quot;B&quot;:5,&quot;C&quot;:8},&quot;z&quot;:{&quot;A&quot;:3,&quot;B&quot;:6,&quot;C&quot;:9}}&apos;</span>

<span class="gp">In [177]: </span><span class="n">sjo</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">orient</span><span class="o">=</span><span class="s2">&quot;index&quot;</span><span class="p">)</span>
<span class="gr">Out[177]: </span><span class="s1">&apos;{&quot;x&quot;:15,&quot;y&quot;:16,&quot;z&quot;:17}&apos;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-740"><strong>面向记录</strong>将数据序列化为列 - &gt;值记录的JSON数组，不包括索引标签。</span><span class="yiyi-st" id="yiyi-741">这对于将DataFrame数据传递到绘图库很有用，例如JavaScript库d3.js：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [178]: </span><span class="n">dfjo</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">orient</span><span class="o">=</span><span class="s2">&quot;records&quot;</span><span class="p">)</span>
<span class="gr">Out[178]: </span><span class="s1">&apos;[{&quot;A&quot;:1,&quot;B&quot;:4,&quot;C&quot;:7},{&quot;A&quot;:2,&quot;B&quot;:5,&quot;C&quot;:8},{&quot;A&quot;:3,&quot;B&quot;:6,&quot;C&quot;:9}]&apos;</span>

<span class="gp">In [179]: </span><span class="n">sjo</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">orient</span><span class="o">=</span><span class="s2">&quot;records&quot;</span><span class="p">)</span>
<span class="gr">Out[179]: </span><span class="s1">&apos;[15,16,17]&apos;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-742"><strong>面向价值</strong>是一个裸体选项，仅序列化为嵌套JSON数组的值，不包括列和索引标签：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [180]: </span><span class="n">dfjo</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">orient</span><span class="o">=</span><span class="s2">&quot;values&quot;</span><span class="p">)</span>
<span class="gr">Out[180]: </span><span class="s1">&apos;[[1,4,7],[2,5,8],[3,6,9]]&apos;</span>

</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-743"><strong>面向分组</strong>序列化为包含值，索引和列的单独条目的JSON对象。</span><span class="yiyi-st" id="yiyi-744"><code class="docutils literal"><span class="pre">Series</span></code>也包括名称：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [181]: </span><span class="n">dfjo</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">orient</span><span class="o">=</span><span class="s2">&quot;split&quot;</span><span class="p">)</span>
<span class="gr">Out[181]: </span><span class="s1">&apos;{&quot;columns&quot;:[&quot;A&quot;,&quot;B&quot;,&quot;C&quot;],&quot;index&quot;:[&quot;x&quot;,&quot;y&quot;,&quot;z&quot;],&quot;data&quot;:[[1,4,7],[2,5,8],[3,6,9]]}&apos;</span>

<span class="gp">In [182]: </span><span class="n">sjo</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">orient</span><span class="o">=</span><span class="s2">&quot;split&quot;</span><span class="p">)</span>
<span class="gr">Out[182]: </span><span class="s1">&apos;{&quot;name&quot;:&quot;D&quot;,&quot;index&quot;:[&quot;x&quot;,&quot;y&quot;,&quot;z&quot;],&quot;data&quot;:[15,16,17]}&apos;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-745">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-746">编码到JSON对象的任何orient选项将不会在往返序列化期间保留索引和列标签的顺序。</span><span class="yiyi-st" id="yiyi-747">如果您希望保留标签排序，请使用<cite>拆分</cite>选项，因为它使用有序容器。</span></p>
</div>
</div>
<div class="section" id="id1">
<h4><span class="yiyi-st" id="yiyi-748">Date Handling</span></h4>
<p><span class="yiyi-st" id="yiyi-749">以ISO日期格式写入</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [183]: </span><span class="n">dfd</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;AB&apos;</span><span class="p">))</span>

<span class="gp">In [184]: </span><span class="n">dfd</span><span class="p">[</span><span class="s1">&apos;date&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">)</span>

<span class="gp">In [185]: </span><span class="n">dfd</span> <span class="o">=</span> <span class="n">dfd</span><span class="o">.</span><span class="n">sort_index</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">ascending</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

<span class="gp">In [186]: </span><span class="n">json</span> <span class="o">=</span> <span class="n">dfd</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">date_format</span><span class="o">=</span><span class="s1">&apos;iso&apos;</span><span class="p">)</span>

<span class="gp">In [187]: </span><span class="n">json</span>
<span class="gr">Out[187]: </span><span class="s1">&apos;{&quot;date&quot;:{&quot;0&quot;:&quot;2013-01-01T00:00:00.000Z&quot;,&quot;1&quot;:&quot;2013-01-01T00:00:00.000Z&quot;,&quot;2&quot;:&quot;2013-01-01T00:00:00.000Z&quot;,&quot;3&quot;:&quot;2013-01-01T00:00:00.000Z&quot;,&quot;4&quot;:&quot;2013-01-01T00:00:00.000Z&quot;},&quot;B&quot;:{&quot;0&quot;:2.5656459463,&quot;1&quot;:1.3403088498,&quot;2&quot;:-0.2261692849,&quot;3&quot;:0.8138502857,&quot;4&quot;:-0.8273169356},&quot;A&quot;:{&quot;0&quot;:-1.2064117817,&quot;1&quot;:1.4312559863,&quot;2&quot;:-1.1702987971,&quot;3&quot;:0.4108345112,&quot;4&quot;:0.1320031703}}&apos;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-750">以ISO日期格式写入，以微秒为单位</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [188]: </span><span class="n">json</span> <span class="o">=</span> <span class="n">dfd</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">date_format</span><span class="o">=</span><span class="s1">&apos;iso&apos;</span><span class="p">,</span> <span class="n">date_unit</span><span class="o">=</span><span class="s1">&apos;us&apos;</span><span class="p">)</span>

<span class="gp">In [189]: </span><span class="n">json</span>
<span class="gr">Out[189]: </span><span class="s1">&apos;{&quot;date&quot;:{&quot;0&quot;:&quot;2013-01-01T00:00:00.000000Z&quot;,&quot;1&quot;:&quot;2013-01-01T00:00:00.000000Z&quot;,&quot;2&quot;:&quot;2013-01-01T00:00:00.000000Z&quot;,&quot;3&quot;:&quot;2013-01-01T00:00:00.000000Z&quot;,&quot;4&quot;:&quot;2013-01-01T00:00:00.000000Z&quot;},&quot;B&quot;:{&quot;0&quot;:2.5656459463,&quot;1&quot;:1.3403088498,&quot;2&quot;:-0.2261692849,&quot;3&quot;:0.8138502857,&quot;4&quot;:-0.8273169356},&quot;A&quot;:{&quot;0&quot;:-1.2064117817,&quot;1&quot;:1.4312559863,&quot;2&quot;:-1.1702987971,&quot;3&quot;:0.4108345112,&quot;4&quot;:0.1320031703}}&apos;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-751">Epoch时间戳，以秒为单位</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [190]: </span><span class="n">json</span> <span class="o">=</span> <span class="n">dfd</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">date_format</span><span class="o">=</span><span class="s1">&apos;epoch&apos;</span><span class="p">,</span> <span class="n">date_unit</span><span class="o">=</span><span class="s1">&apos;s&apos;</span><span class="p">)</span>

<span class="gp">In [191]: </span><span class="n">json</span>
<span class="gr">Out[191]: </span><span class="s1">&apos;{&quot;date&quot;:{&quot;0&quot;:1356998400,&quot;1&quot;:1356998400,&quot;2&quot;:1356998400,&quot;3&quot;:1356998400,&quot;4&quot;:1356998400},&quot;B&quot;:{&quot;0&quot;:2.5656459463,&quot;1&quot;:1.3403088498,&quot;2&quot;:-0.2261692849,&quot;3&quot;:0.8138502857,&quot;4&quot;:-0.8273169356},&quot;A&quot;:{&quot;0&quot;:-1.2064117817,&quot;1&quot;:1.4312559863,&quot;2&quot;:-1.1702987971,&quot;3&quot;:0.4108345112,&quot;4&quot;:0.1320031703}}&apos;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-752">写入文件，包含日期索引和日期列</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [192]: </span><span class="n">dfj2</span> <span class="o">=</span> <span class="n">dfj</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

<span class="gp">In [193]: </span><span class="n">dfj2</span><span class="p">[</span><span class="s1">&apos;date&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">)</span>

<span class="gp">In [194]: </span><span class="n">dfj2</span><span class="p">[</span><span class="s1">&apos;ints&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>

<span class="gp">In [195]: </span><span class="n">dfj2</span><span class="p">[</span><span class="s1">&apos;bools&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span>

<span class="gp">In [196]: </span><span class="n">dfj2</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>

<span class="gp">In [197]: </span><span class="n">dfj2</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="s1">&apos;test.json&apos;</span><span class="p">)</span>

<span class="gp">In [198]: </span><span class="nb">open</span><span class="p">(</span><span class="s1">&apos;test.json&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="gr">Out[198]: </span><span class="s1">&apos;{&quot;A&quot;:{&quot;1356998400000&quot;:-1.2945235903,&quot;1357084800000&quot;:0.2766617129,&quot;1357171200000&quot;:-0.0139597524,&quot;1357257600000&quot;:-0.0061535699,&quot;1357344000000&quot;:0.8957173022},&quot;B&quot;:{&quot;1356998400000&quot;:0.4137381054,&quot;1357084800000&quot;:-0.472034511,&quot;1357171200000&quot;:-0.3625429925,&quot;1357257600000&quot;:-0.923060654,&quot;1357344000000&quot;:0.8052440254},&quot;date&quot;:{&quot;1356998400000&quot;:1356998400000,&quot;1357084800000&quot;:1356998400000,&quot;1357171200000&quot;:1356998400000,&quot;1357257600000&quot;:1356998400000,&quot;1357344000000&quot;:1356998400000},&quot;ints&quot;:{&quot;1356998400000&quot;:0,&quot;1357084800000&quot;:1,&quot;1357171200000&quot;:2,&quot;1357257600000&quot;:3,&quot;1357344000000&quot;:4},&quot;bools&quot;:{&quot;1356998400000&quot;:true,&quot;1357084800000&quot;:true,&quot;1357171200000&quot;:true,&quot;1357257600000&quot;:true,&quot;1357344000000&quot;:true}}&apos;</span>
</pre></div>
</div>
</div>
<div class="section" id="fallback-behavior">
<h4><span class="yiyi-st" id="yiyi-753">Fallback Behavior</span></h4>
<p><span class="yiyi-st" id="yiyi-754">如果JSON序列化器不能直接处理容器内容，它将以以下方式回退：</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-755">如果dtype不被支持（例如<code class="docutils literal"><span class="pre">np.complex</span></code>），那么将为每个值调用<code class="docutils literal"><span class="pre">default_handler</span></code>（如果提供），否则会引发异常。</span></li>
<li><span class="yiyi-st" id="yiyi-761">如果对象不受支持，它将尝试以下操作：</span><ul>
<li><span class="yiyi-st" id="yiyi-756">检查对象是否定义了<code class="docutils literal"><span class="pre">toDict</span></code>方法并调用它。</span><span class="yiyi-st" id="yiyi-757"><code class="docutils literal"><span class="pre">toDict</span></code>方法应返回一个<code class="docutils literal"><span class="pre">dict</span></code>，然后将JSON序列化。</span></li>
<li><span class="yiyi-st" id="yiyi-758">调用<code class="docutils literal"><span class="pre">default_handler</span></code>（如果提供）。</span></li>
<li><span class="yiyi-st" id="yiyi-759">通过遍历其内容将对象转换为<code class="docutils literal"><span class="pre">dict</span></code>。</span><span class="yiyi-st" id="yiyi-760">但是，这通常会失败并显示<code class="docutils literal"><span class="pre">OverflowError</span></code>或给出意外的结果。</span></li>
</ul>
</li>
</ul>
<p><span class="yiyi-st" id="yiyi-762">一般来说，不受支持的对象或类型的最佳方法是提供一个<code class="docutils literal"><span class="pre">default_handler</span></code>。</span><span class="yiyi-st" id="yiyi-763">例如：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">DataFrame</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="nb">complex</span><span class="p">(</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">)])</span><span class="o">.</span><span class="n">to_json</span><span class="p">()</span>  <span class="c1"># raises</span>

<span class="ne">RuntimeError</span><span class="p">:</span> <span class="n">Unhandled</span> <span class="n">numpy</span> <span class="n">dtype</span> <span class="mi">15</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-764">可以通过指定一个简单的<code class="docutils literal"><span class="pre">default_handler</span></code>来处理：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [199]: </span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="nb">complex</span><span class="p">(</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">)])</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">default_handler</span><span class="o">=</span><span class="nb">str</span><span class="p">)</span>
<span class="gr">Out[199]: </span><span class="s1">&apos;{&quot;0&quot;:{&quot;0&quot;:&quot;(1+0j)&quot;,&quot;1&quot;:&quot;(2+0j)&quot;,&quot;2&quot;:&quot;(1+2j)&quot;}}&apos;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="reading-json">
<span id="io-json-reader"></span><h3><span class="yiyi-st" id="yiyi-765">Reading JSON</span></h3>
<p><span class="yiyi-st" id="yiyi-766">读取JSON字符串到pandas对象可以取多个参数。</span><span class="yiyi-st" id="yiyi-767">如果未提供<code class="docutils literal"><span class="pre">typ</span></code>或<code class="docutils literal"><span class="pre">None</span></code>，解析器将尝试解析<code class="docutils literal"><span class="pre">DataFrame</span></code>。</span><span class="yiyi-st" id="yiyi-768">要明确强制<code class="docutils literal"><span class="pre">Series</span></code>解析，请传递<code class="docutils literal"><span class="pre">typ=series</span></code></span></p>
<ul>
<li><p class="first"><span class="yiyi-st" id="yiyi-769"><code class="docutils literal"><span class="pre">filepath_or_buffer</span></code>：a <strong>VALID</strong> JSON字符串或文件句柄/ StringIO。</span><span class="yiyi-st" id="yiyi-770">字符串可以是URL。</span><span class="yiyi-st" id="yiyi-771">有效的URL方案包括http，ftp，S3和文件。</span><span class="yiyi-st" id="yiyi-772">对于文件URL，需要主机。</span><span class="yiyi-st" id="yiyi-773">例如，本地文件可以是file：//localhost/path/to/table.json</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-774"><code class="docutils literal"><span class="pre">typ</span></code>：要恢复的对象类型（系列或框架），默认&apos;frame&apos;</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-775"><code class="docutils literal"><span class="pre">orient</span></code>：</span></p>
<dl class="docutils">
<dt><span class="yiyi-st" id="yiyi-776">系列：</span></dt>
<dd><ul class="first last simple">
<li><span class="yiyi-st" id="yiyi-777">默认为<code class="docutils literal"><span class="pre">index</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-778">允许值为{<code class="docutils literal"><span class="pre">split</span></code>，<code class="docutils literal"><span class="pre">records</span></code>，<code class="docutils literal"><span class="pre">index</span></code>}</span></li>
</ul>
</dd>
<dt><span class="yiyi-st" id="yiyi-779">DataFrame</span></dt>
<dd><ul class="first last simple">
<li><span class="yiyi-st" id="yiyi-780">默认为<code class="docutils literal"><span class="pre">columns</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-781">允许值为{<code class="docutils literal"><span class="pre">split</span></code>，<code class="docutils literal"><span class="pre">records</span></code>，<code class="docutils literal"><span class="pre">index</span></code>，<code class="docutils literal"><span class="pre">columns</span></code>，<code class="docutils literal"><span class="pre">values</span></code></span></li>
</ul>
</dd>
</dl>
<p><span class="yiyi-st" id="yiyi-782">JSON字符串的格式</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="12%">
<col width="88%">
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-783"><code class="docutils literal"><span class="pre">split</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-784">dict like {index  - &gt; [index]，columns  - &gt; [columns]，data  - &gt; [values]}</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-785"><code class="docutils literal"><span class="pre">records</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-786">列表像[{column  - &gt; value}，...，{column  - &gt; value}]</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-787"><code class="docutils literal"><span class="pre">index</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-788">dict like {index  - &gt; {column  - &gt; value}}</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-789"><code class="docutils literal"><span class="pre">columns</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-790">dict like {column  - &gt; {index  - &gt; value}}</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-791"><code class="docutils literal"><span class="pre">values</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-792">只是值数组</span></td>
</tr>
</tbody>
</table>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-793"><code class="docutils literal"><span class="pre">dtype</span></code>：如果为True，推断dtypes，如果一个列的字典到dtype，然后使用那些，如果False，则不推断dtypes在所有，默认为True，仅适用于数据</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-794"><code class="docutils literal"><span class="pre">convert_axes</span></code>：boolean，尝试将轴转换为正确的dtypes，默认值为True</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-795"><code class="docutils literal"><span class="pre">convert_dates</span></code>：解析日期的列列表；如果为True，则尝试解析类似日期的列，默认值为True</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-796"><code class="docutils literal"><span class="pre">keep_default_dates</span></code>：boolean，default True。</span><span class="yiyi-st" id="yiyi-797">如果解析日期，则解析默认的日期样列</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-798"><code class="docutils literal"><span class="pre">numpy</span></code>：直接解码为numpy数组。</span><span class="yiyi-st" id="yiyi-799">默认为False；仅支持数字数据，但标签可能是非数字的。</span><span class="yiyi-st" id="yiyi-800">还要注意，如果<code class="docutils literal"><span class="pre">numpy=True</span></code>，JSON排序<strong>MUST</strong></span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-801"><code class="docutils literal"><span class="pre">precise_float</span></code>：boolean，默认<code class="docutils literal"><span class="pre">False</span></code>。</span><span class="yiyi-st" id="yiyi-802">设置为在将字符串解码为双精度值时启用更高精度（strtod）函数的使用。</span><span class="yiyi-st" id="yiyi-803">默认值（<code class="docutils literal"><span class="pre">False</span></code>）是使用快速但不太精确的内置功能</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-804"><code class="docutils literal"><span class="pre">date_unit</span></code>：string，用于检测转换日期的时间戳单位。</span><span class="yiyi-st" id="yiyi-805">默认值无。</span><span class="yiyi-st" id="yiyi-806">默认情况下，将检测时间戳精度，如果不需要，则通过&apos;s&apos;，&apos;ms&apos;，&apos;us&apos;或&apos;ns&apos;之一分别强制时间戳精度为秒，毫秒，微秒或纳秒。</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-807"><code class="docutils literal"><span class="pre">lines</span></code>：每行将文件读取为一个json对象。</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-808"><code class="docutils literal"><span class="pre">encoding</span></code>：用于解码py3字节的编码。</span></p>
</li>
</ul>
<p><span class="yiyi-st" id="yiyi-809">如果JSON不可解析，解析器将产生<code class="docutils literal"><span class="pre">ValueError/TypeError/AssertionError</span></code>之一。</span></p>
<p><span class="yiyi-st" id="yiyi-810">如果在编码为JSON时使用非默认<code class="docutils literal"><span class="pre">orient</span></code>，则必须在此处传递相同的选项，以便解码产生合理的结果，有关概述，请参见<a class="reference internal" href="#orient-options">Orient选项</a>。</span></p>
<div class="section" id="data-conversion">
<h4><span class="yiyi-st" id="yiyi-811">Data Conversion</span></h4>
<p><span class="yiyi-st" id="yiyi-812">默认为<code class="docutils literal"><span class="pre">convert_axes=True</span></code>，<code class="docutils literal"><span class="pre">dtype=True</span></code>和<code class="docutils literal"><span class="pre">convert_dates=True</span></code>会尝试解析轴，类型，包括日期。</span><span class="yiyi-st" id="yiyi-813">如果您需要覆盖特定的dtypes，请传递一个dict到<code class="docutils literal"><span class="pre">dtype</span></code>。</span><span class="yiyi-st" id="yiyi-814">如果您需要在轴中保留字符串状数字（例如“1”，“2”），则只应将<code class="docutils literal"><span class="pre">convert_axes</span></code>设置为<code class="docutils literal"><span class="pre">False</span></code>。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-815">注意</span></p>
<p><span class="yiyi-st" id="yiyi-816">如果<code class="docutils literal"><span class="pre">convert_dates=True</span></code>，且数据和/或列标签显示为“日期样”，则大整数值可以转换为日期。</span><span class="yiyi-st" id="yiyi-817">确切的阈值取决于指定的<code class="docutils literal"><span class="pre">date_unit</span></code>。</span><span class="yiyi-st" id="yiyi-818">“日期样”表示列标签满足以下条件之一：</span></p>
<blockquote class="last">
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-819">它以<code class="docutils literal"><span class="pre">&apos;_at&apos;</span></code>结尾</span></li>
<li><span class="yiyi-st" id="yiyi-820">它以<code class="docutils literal"><span class="pre">&apos;_time&apos;</span></code>结尾</span></li>
<li><span class="yiyi-st" id="yiyi-821">它从<code class="docutils literal"><span class="pre">&apos;timestamp&apos;</span></code>开始</span></li>
<li><span class="yiyi-st" id="yiyi-822">它是<code class="docutils literal"><span class="pre">&apos;modified&apos;</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-823">它是<code class="docutils literal"><span class="pre">&apos;date&apos;</span></code></span></li>
</ul>
</div></blockquote>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-824">警告</span></p>
<p><span class="yiyi-st" id="yiyi-825">当读取JSON数据时，自动强制转换为dtypes有一些怪癖：</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-826">可以按照与序列化不同的顺序重建索引，也就是说，返回的顺序不能保证与序列化之前相同</span></li>
<li><span class="yiyi-st" id="yiyi-827">如果可以安全地执行，则<code class="docutils literal"><span class="pre">float</span></code>数据的列将转换为<code class="docutils literal"><span class="pre">integer</span></code>。一列<code class="docutils literal"><span class="pre">1.</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-828">bool列将在重建时转换为<code class="docutils literal"><span class="pre">integer</span></code></span></li>
</ul>
</div></blockquote>
<p class="last"><span class="yiyi-st" id="yiyi-829">因此，有时您可能需要通过<code class="docutils literal"><span class="pre">dtype</span></code>关键字参数指定特定的dtypes。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-830">从JSON字符串读取：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [200]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="n">json</span><span class="p">)</span>
<span class="gr">Out[200]: </span>
<span class="go">          A         B       date</span>
<span class="go">0 -1.206412  2.565646 2013-01-01</span>
<span class="go">1  1.431256  1.340309 2013-01-01</span>
<span class="go">2 -1.170299 -0.226169 2013-01-01</span>
<span class="go">3  0.410835  0.813850 2013-01-01</span>
<span class="go">4  0.132003 -0.827317 2013-01-01</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-831">从文件读取：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [201]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="s1">&apos;test.json&apos;</span><span class="p">)</span>
<span class="gr">Out[201]: </span>
<span class="go">                   A         B bools       date  ints</span>
<span class="go">2013-01-01 -1.294524  0.413738  True 2013-01-01     0</span>
<span class="go">2013-01-02  0.276662 -0.472035  True 2013-01-01     1</span>
<span class="go">2013-01-03 -0.013960 -0.362543  True 2013-01-01     2</span>
<span class="go">2013-01-04 -0.006154 -0.923061  True 2013-01-01     3</span>
<span class="go">2013-01-05  0.895717  0.805244  True 2013-01-01     4</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-832">不转换任何数据（但仍然转换轴和日期）：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [202]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="s1">&apos;test.json&apos;</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">object</span><span class="p">)</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[202]: </span>
<span class="go">A        object</span>
<span class="go">B        object</span>
<span class="go">bools    object</span>
<span class="go">date     object</span>
<span class="go">ints     object</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-833">指定转换的类型：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [203]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="s1">&apos;test.json&apos;</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;A&apos;</span> <span class="p">:</span> <span class="s1">&apos;float32&apos;</span><span class="p">,</span> <span class="s1">&apos;bools&apos;</span> <span class="p">:</span> <span class="s1">&apos;int8&apos;</span><span class="p">})</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[203]: </span>
<span class="go">A               float32</span>
<span class="go">B               float64</span>
<span class="go">bools              int8</span>
<span class="go">date     datetime64[ns]</span>
<span class="go">ints              int64</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-834">保留字符串索引：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [204]: </span><span class="n">si</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">)),</span>
<span class="gp">   .....:</span>          <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">)),</span>
<span class="gp">   .....:</span>          <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">)])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [205]: </span><span class="n">si</span>
<span class="gr">Out[205]: </span>
<span class="go">     0    1    2    3</span>
<span class="go">0  0.0  0.0  0.0  0.0</span>
<span class="go">1  0.0  0.0  0.0  0.0</span>
<span class="go">2  0.0  0.0  0.0  0.0</span>
<span class="go">3  0.0  0.0  0.0  0.0</span>

<span class="gp">In [206]: </span><span class="n">si</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[206]: </span><span class="n">Index</span><span class="p">([</span><span class="s1">u&apos;0&apos;</span><span class="p">,</span> <span class="s1">u&apos;1&apos;</span><span class="p">,</span> <span class="s1">u&apos;2&apos;</span><span class="p">,</span> <span class="s1">u&apos;3&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;object&apos;</span><span class="p">)</span>

<span class="gp">In [207]: </span><span class="n">si</span><span class="o">.</span><span class="n">columns</span>
<span class="gr">Out[207]: </span><span class="n">Int64Index</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;int64&apos;</span><span class="p">)</span>

<span class="gp">In [208]: </span><span class="n">json</span> <span class="o">=</span> <span class="n">si</span><span class="o">.</span><span class="n">to_json</span><span class="p">()</span>

<span class="gp">In [209]: </span><span class="n">sij</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="n">json</span><span class="p">,</span> <span class="n">convert_axes</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

<span class="gp">In [210]: </span><span class="n">sij</span>
<span class="gr">Out[210]: </span>
<span class="go">   0  1  2  3</span>
<span class="go">0  0  0  0  0</span>
<span class="go">1  0  0  0  0</span>
<span class="go">2  0  0  0  0</span>
<span class="go">3  0  0  0  0</span>

<span class="gp">In [211]: </span><span class="n">sij</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[211]: </span><span class="n">Index</span><span class="p">([</span><span class="s1">u&apos;0&apos;</span><span class="p">,</span> <span class="s1">u&apos;1&apos;</span><span class="p">,</span> <span class="s1">u&apos;2&apos;</span><span class="p">,</span> <span class="s1">u&apos;3&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;object&apos;</span><span class="p">)</span>

<span class="gp">In [212]: </span><span class="n">sij</span><span class="o">.</span><span class="n">columns</span>
<span class="gr">Out[212]: </span><span class="n">Index</span><span class="p">([</span><span class="s1">u&apos;0&apos;</span><span class="p">,</span> <span class="s1">u&apos;1&apos;</span><span class="p">,</span> <span class="s1">u&apos;2&apos;</span><span class="p">,</span> <span class="s1">u&apos;3&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;object&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-835">以纳秒为单位写入的日期需要以纳秒为单位进行读取：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [213]: </span><span class="n">json</span> <span class="o">=</span> <span class="n">dfj2</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">date_unit</span><span class="o">=</span><span class="s1">&apos;ns&apos;</span><span class="p">)</span>

<span class="c"># Try to parse timestamps as millseconds -&gt; Won&apos;t Work</span>
<span class="gp">In [214]: </span><span class="n">dfju</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="n">json</span><span class="p">,</span> <span class="n">date_unit</span><span class="o">=</span><span class="s1">&apos;ms&apos;</span><span class="p">)</span>

<span class="gp">In [215]: </span><span class="n">dfju</span>
<span class="gr">Out[215]: </span>
<span class="go">                            A         B bools                 date  ints</span>
<span class="go">1356998400000000000 -1.294524  0.413738  True  1356998400000000000     0</span>
<span class="go">1357084800000000000  0.276662 -0.472035  True  1356998400000000000     1</span>
<span class="go">1357171200000000000 -0.013960 -0.362543  True  1356998400000000000     2</span>
<span class="go">1357257600000000000 -0.006154 -0.923061  True  1356998400000000000     3</span>
<span class="go">1357344000000000000  0.895717  0.805244  True  1356998400000000000     4</span>

<span class="c"># Let pandas detect the correct precision</span>
<span class="gp">In [216]: </span><span class="n">dfju</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="n">json</span><span class="p">)</span>

<span class="gp">In [217]: </span><span class="n">dfju</span>
<span class="gr">Out[217]: </span>
<span class="go">                   A         B bools       date  ints</span>
<span class="go">2013-01-01 -1.294524  0.413738  True 2013-01-01     0</span>
<span class="go">2013-01-02  0.276662 -0.472035  True 2013-01-01     1</span>
<span class="go">2013-01-03 -0.013960 -0.362543  True 2013-01-01     2</span>
<span class="go">2013-01-04 -0.006154 -0.923061  True 2013-01-01     3</span>
<span class="go">2013-01-05  0.895717  0.805244  True 2013-01-01     4</span>

<span class="c"># Or specify that all timestamps are in nanoseconds</span>
<span class="gp">In [218]: </span><span class="n">dfju</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="n">json</span><span class="p">,</span> <span class="n">date_unit</span><span class="o">=</span><span class="s1">&apos;ns&apos;</span><span class="p">)</span>

<span class="gp">In [219]: </span><span class="n">dfju</span>
<span class="gr">Out[219]: </span>
<span class="go">                   A         B bools       date  ints</span>
<span class="go">2013-01-01 -1.294524  0.413738  True 2013-01-01     0</span>
<span class="go">2013-01-02  0.276662 -0.472035  True 2013-01-01     1</span>
<span class="go">2013-01-03 -0.013960 -0.362543  True 2013-01-01     2</span>
<span class="go">2013-01-04 -0.006154 -0.923061  True 2013-01-01     3</span>
<span class="go">2013-01-05  0.895717  0.805244  True 2013-01-01     4</span>
</pre></div>
</div>
</div>
<div class="section" id="the-numpy-parameter">
<h4><span class="yiyi-st" id="yiyi-836">The Numpy Parameter</span></h4>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-837">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-838">这仅支持数值数据。</span><span class="yiyi-st" id="yiyi-839">索引和列标签可以是非数字的，例如。字符串，日期等。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-840">如果<code class="docutils literal"><span class="pre">numpy=True</span></code>被传递到<code class="docutils literal"><span class="pre">read_json</span></code>，将尝试在反序列化期间侦听适当的dtype，然后直接解码为numpy数组，绕过对中间Python对象。</span></p>
<p><span class="yiyi-st" id="yiyi-841">如果你反序列化大量的数值数据，这可以提供加速：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [220]: </span><span class="n">randfloats</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="o">-</span><span class="mi">100</span><span class="p">,</span> <span class="mi">1000</span><span class="p">,</span> <span class="mi">10000</span><span class="p">)</span>

<span class="gp">In [221]: </span><span class="n">randfloats</span><span class="o">.</span><span class="n">shape</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>

<span class="gp">In [222]: </span><span class="n">dffloats</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randfloats</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;ABCDEFGHIJ&apos;</span><span class="p">))</span>

<span class="gp">In [223]: </span><span class="n">jsonfloats</span> <span class="o">=</span> <span class="n">dffloats</span><span class="o">.</span><span class="n">to_json</span><span class="p">()</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [224]: </span><span class="n">timeit</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="n">jsonfloats</span><span class="p">)</span>
<span class="go">100 loops, best of 3: 12.2 ms per loop</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [225]: </span><span class="n">timeit</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="n">jsonfloats</span><span class="p">,</span> <span class="n">numpy</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="go">100 loops, best of 3: 7.35 ms per loop</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-842">对于较小的数据集，加速不太明显：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [226]: </span><span class="n">jsonfloats</span> <span class="o">=</span> <span class="n">dffloats</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span><span class="o">.</span><span class="n">to_json</span><span class="p">()</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [227]: </span><span class="n">timeit</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="n">jsonfloats</span><span class="p">)</span>
<span class="go">100 loops, best of 3: 5.72 ms per loop</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [228]: </span><span class="n">timeit</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="n">jsonfloats</span><span class="p">,</span> <span class="n">numpy</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="go">100 loops, best of 3: 4.94 ms per loop</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-843">警告</span></p>
<p><span class="yiyi-st" id="yiyi-844">直接numpy解码进行了许多假设，如果不满足这些假设，可能会失败或产生意外的输出：</span></p>
<blockquote class="last">
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-845">数据是数字。</span></li>
<li><span class="yiyi-st" id="yiyi-846">数据是均匀的。</span><span class="yiyi-st" id="yiyi-847">dtype从解码的第一个值被嗅探。</span><span class="yiyi-st" id="yiyi-848">可能会出现<code class="docutils literal"><span class="pre">ValueError</span></code>，如果不满足此条件，可能会产生不正确的输出。</span></li>
<li><span class="yiyi-st" id="yiyi-849">标签是有序的。</span><span class="yiyi-st" id="yiyi-850">仅从第一个容器中读取标签，假定每个后续行/列都按相同的顺序进行编码。</span><span class="yiyi-st" id="yiyi-851">如果数据使用<code class="docutils literal"><span class="pre">to_json</span></code>编码，则应满足此要求，但如果JSON来自其他来源，则可能不是这样。</span></li>
</ul>
</div></blockquote>
</div>
</div>
</div>
<div class="section" id="normalization">
<span id="io-json-normalize"></span><h3><span class="yiyi-st" id="yiyi-852">Normalization</span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-853"><span class="versionmodified">版本0.13.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-854">pandas提供了一个实用函数来接受一个dict或list的列表，并将这个半结构化数据<em>正规化</em>到一个平面表中。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [229]: </span><span class="kn">from</span> <span class="nn">pandas.io.json</span> <span class="kn">import</span> <span class="n">json_normalize</span>

<span class="gp">In [230]: </span><span class="n">data</span> <span class="o">=</span> <span class="p">[{</span><span class="s1">&apos;state&apos;</span><span class="p">:</span> <span class="s1">&apos;Florida&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>           <span class="s1">&apos;shortname&apos;</span><span class="p">:</span> <span class="s1">&apos;FL&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>           <span class="s1">&apos;info&apos;</span><span class="p">:</span> <span class="p">{</span>
<span class="gp">   .....:</span>                <span class="s1">&apos;governor&apos;</span><span class="p">:</span> <span class="s1">&apos;Rick Scott&apos;</span>
<span class="gp">   .....:</span>           <span class="p">},</span>
<span class="gp">   .....:</span>           <span class="s1">&apos;counties&apos;</span><span class="p">:</span> <span class="p">[{</span><span class="s1">&apos;name&apos;</span><span class="p">:</span> <span class="s1">&apos;Dade&apos;</span><span class="p">,</span> <span class="s1">&apos;population&apos;</span><span class="p">:</span> <span class="mi">12345</span><span class="p">},</span>
<span class="gp">   .....:</span>                       <span class="p">{</span><span class="s1">&apos;name&apos;</span><span class="p">:</span> <span class="s1">&apos;Broward&apos;</span><span class="p">,</span> <span class="s1">&apos;population&apos;</span><span class="p">:</span> <span class="mi">40000</span><span class="p">},</span>
<span class="gp">   .....:</span>                       <span class="p">{</span><span class="s1">&apos;name&apos;</span><span class="p">:</span> <span class="s1">&apos;Palm Beach&apos;</span><span class="p">,</span> <span class="s1">&apos;population&apos;</span><span class="p">:</span> <span class="mi">60000</span><span class="p">}]},</span>
<span class="gp">   .....:</span>          <span class="p">{</span><span class="s1">&apos;state&apos;</span><span class="p">:</span> <span class="s1">&apos;Ohio&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>           <span class="s1">&apos;shortname&apos;</span><span class="p">:</span> <span class="s1">&apos;OH&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>           <span class="s1">&apos;info&apos;</span><span class="p">:</span> <span class="p">{</span>
<span class="gp">   .....:</span>                <span class="s1">&apos;governor&apos;</span><span class="p">:</span> <span class="s1">&apos;John Kasich&apos;</span>
<span class="gp">   .....:</span>           <span class="p">},</span>
<span class="gp">   .....:</span>           <span class="s1">&apos;counties&apos;</span><span class="p">:</span> <span class="p">[{</span><span class="s1">&apos;name&apos;</span><span class="p">:</span> <span class="s1">&apos;Summit&apos;</span><span class="p">,</span> <span class="s1">&apos;population&apos;</span><span class="p">:</span> <span class="mi">1234</span><span class="p">},</span>
<span class="gp">   .....:</span>                        <span class="p">{</span><span class="s1">&apos;name&apos;</span><span class="p">:</span> <span class="s1">&apos;Cuyahoga&apos;</span><span class="p">,</span> <span class="s1">&apos;population&apos;</span><span class="p">:</span> <span class="mi">1337</span><span class="p">}]}]</span>
<span class="gp">   .....:</span> 

<span class="gp">In [231]: </span><span class="n">json_normalize</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="s1">&apos;counties&apos;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&apos;state&apos;</span><span class="p">,</span> <span class="s1">&apos;shortname&apos;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&apos;info&apos;</span><span class="p">,</span> <span class="s1">&apos;governor&apos;</span><span class="p">]])</span>
<span class="gr">Out[231]: </span>
<span class="go">         name  population info.governor    state shortname</span>
<span class="go">0        Dade       12345    Rick Scott  Florida        FL</span>
<span class="go">1     Broward       40000    Rick Scott  Florida        FL</span>
<span class="go">2  Palm Beach       60000    Rick Scott  Florida        FL</span>
<span class="go">3      Summit        1234   John Kasich     Ohio        OH</span>
<span class="go">4    Cuyahoga        1337   John Kasich     Ohio        OH</span>
</pre></div>
</div>
</div>
<div class="section" id="line-delimited-json">
<span id="io-jsonl"></span><h3><span class="yiyi-st" id="yiyi-855">Line delimited json</span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-856"><span class="versionmodified">版本0.19.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-857">pandas能够读取和写入使用Hadoop或Spark在数据处理管道中常见的行分隔的json文件。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [232]: </span><span class="n">jsonl</span> <span class="o">=</span> <span class="s1">&apos;&apos;&apos;</span>
<span class="gp">   .....:</span><span class="s1">     {&quot;a&quot;:1,&quot;b&quot;:2}</span>
<span class="gp">   .....:</span><span class="s1">     {&quot;a&quot;:3,&quot;b&quot;:4}</span>
<span class="gp">   .....:</span><span class="s1"> &apos;&apos;&apos;</span>
<span class="gp">   .....:</span> 

<span class="gp">In [233]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_json</span><span class="p">(</span><span class="n">jsonl</span><span class="p">,</span> <span class="n">lines</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [234]: </span><span class="n">df</span>
<span class="gr">Out[234]: </span>
<span class="go">   a  b</span>
<span class="go">0  1  2</span>
<span class="go">1  3  4</span>

<span class="gp">In [235]: </span><span class="n">df</span><span class="o">.</span><span class="n">to_json</span><span class="p">(</span><span class="n">orient</span><span class="o">=</span><span class="s1">&apos;records&apos;</span><span class="p">,</span> <span class="n">lines</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gr">Out[235]: </span><span class="s1">u&apos;{&quot;a&quot;:1,&quot;b&quot;:2}</span><span class="se">\n</span><span class="s1">{&quot;a&quot;:3,&quot;b&quot;:4}&apos;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="html">
<h2><span class="yiyi-st" id="yiyi-858">HTML</span></h2>
<div class="section" id="reading-html-content">
<span id="io-read-html"></span><h3><span class="yiyi-st" id="yiyi-859">Reading HTML Content</span></h3>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-860">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-861">我们<strong>高度鼓励</strong>您阅读关于BeautifulSoup4 / html5lib / lxml解析器相关问题的<a class="reference internal" href="gotchas.html#html-gotchas"><span class="std std-ref">HTML parsing gotchas</span></a>。</span></p>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-862"><span class="versionmodified">版本0.12.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-863">顶层<code class="xref py py-func docutils literal"><span class="pre">read_html()</span></code>函数可以接受HTML字符串/文件/ URL，并将HTML表解析为pandas DataFrames列表。</span><span class="yiyi-st" id="yiyi-864">让我们来看几个例子。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-865">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-866"><code class="docutils literal"><span class="pre">read_html</span></code>返回<code class="docutils literal"><span class="pre">DataFrame</span></code>对象的<code class="docutils literal"><span class="pre">list</span></code>，即使HTML内容中只包含一个表</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-867">读取没有选项的网址</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [236]: </span><span class="n">url</span> <span class="o">=</span> <span class="s1">&apos;http://www.fdic.gov/bank/individual/failed/banklist.html&apos;</span>

<span class="gp">In [237]: </span><span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>

<span class="gp">In [238]: </span><span class="n">dfs</span>
<span class="gr">Out[238]: </span>
<span class="go">[                             Bank Name             City  ST   CERT  \</span>
<span class="go"> 0                          Allied Bank         Mulberry  AR     91   </span>
<span class="go"> 1         The Woodbury Banking Company         Woodbury  GA  11297   </span>
<span class="go"> 2               First CornerStone Bank  King of Prussia  PA  35312   </span>
<span class="go"> 3                   Trust Company Bank          Memphis  TN   9956   </span>
<span class="go"> 4           North Milwaukee State Bank        Milwaukee  WI  20364   </span>
<span class="go"> 5               Hometown National Bank         Longview  WA  35156   </span>
<span class="go"> 6                  The Bank of Georgia   Peachtree City  GA  35259   </span>
<span class="go"> ..                                 ...              ...  ..    ...   </span>
<span class="go"> 540      Hamilton Bank, NA  En Espanol            Miami  FL  24382   </span>
<span class="go"> 541             Sinclair National Bank         Gravette  AR  34248   </span>
<span class="go"> 542                 Superior Bank, FSB         Hinsdale  IL  32646   </span>
<span class="go"> 543                Malta National Bank            Malta  OH   6629   </span>
<span class="go"> 544    First Alliance Bank &amp; Trust Co.       Manchester  NH  34264   </span>
<span class="go"> 545  National State Bank of Metropolis       Metropolis  IL   3815   </span>
<span class="go"> 546                   Bank of Honolulu         Honolulu  HI  21029   </span>
<span class="go"> </span>
<span class="go">                    Acquiring Institution        Closing Date  \</span>
<span class="go"> 0                           Today&apos;s Bank  September 23, 2016   </span>
<span class="go"> 1                            United Bank     August 19, 2016   </span>
<span class="go"> 2    First-Citizens Bank &amp; Trust Company         May 6, 2016   </span>
<span class="go"> 3             The Bank of Fayette County      April 29, 2016   </span>
<span class="go"> 4    First-Citizens Bank &amp; Trust Company      March 11, 2016   </span>
<span class="go"> 5                         Twin City Bank     October 2, 2015   </span>
<span class="go"> 6                          Fidelity Bank     October 2, 2015   </span>
<span class="go"> ..                                   ...                 ...   </span>
<span class="go"> 540     Israel Discount Bank of New York    January 11, 2002   </span>
<span class="go"> 541                   Delta Trust &amp; Bank   September 7, 2001   </span>
<span class="go"> 542                Superior Federal, FSB       July 27, 2001   </span>
<span class="go"> 543                    North Valley Bank         May 3, 2001   </span>
<span class="go"> 544  Southern New Hampshire Bank &amp; Trust    February 2, 2001   </span>
<span class="go"> 545              Banterra Bank of Marion   December 14, 2000   </span>
<span class="go"> 546                   Bank of the Orient    October 13, 2000   </span>
<span class="go"> </span>
<span class="go">            Updated Date  </span>
<span class="go"> 0     November 17, 2016  </span>
<span class="go"> 1     November 17, 2016  </span>
<span class="go"> 2     September 6, 2016  </span>
<span class="go"> 3     September 6, 2016  </span>
<span class="go"> 4         June 16, 2016  </span>
<span class="go"> 5        April 13, 2016  </span>
<span class="go"> 6      October 24, 2016  </span>
<span class="go"> ..                  ...  </span>
<span class="go"> 540  September 21, 2015  </span>
<span class="go"> 541   February 10, 2004  </span>
<span class="go"> 542     August 19, 2014  </span>
<span class="go"> 543   November 18, 2002  </span>
<span class="go"> 544   February 18, 2003  </span>
<span class="go"> 545      March 17, 2005  </span>
<span class="go"> 546      March 17, 2005  </span>
<span class="go"> </span>
<span class="go"> [547 rows x 7 columns]]</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-868">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-869">来自上述URL的数据每个星期一都会更改，因此上面生成的数据和下面的数据可能会略有不同。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-870">从上述网址读取文件的内容，并将其作为字符串传递给<code class="docutils literal"><span class="pre">read_html</span></code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [239]: </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file_path</span><span class="p">,</span> <span class="s1">&apos;r&apos;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="gp">   .....:</span>     <span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="gp">   .....:</span> 

<span class="gp">In [240]: </span><span class="n">dfs</span>
<span class="gr">Out[240]: </span>
<span class="go">[                                    Bank Name          City  ST   CERT  \</span>
<span class="go"> 0    Banks of Wisconsin d/b/a Bank of Kenosha       Kenosha  WI  35386   </span>
<span class="go"> 1                        Central Arizona Bank    Scottsdale  AZ  34527   </span>
<span class="go"> 2                                Sunrise Bank      Valdosta  GA  58185   </span>
<span class="go"> 3                       Pisgah Community Bank     Asheville  NC  58701   </span>
<span class="go"> 4                         Douglas County Bank  Douglasville  GA  21649   </span>
<span class="go"> 5                                Parkway Bank        Lenoir  NC  57158   </span>
<span class="go"> 6                      Chipola Community Bank      Marianna  FL  58034   </span>
<span class="go"> ..                                        ...           ...  ..    ...   </span>
<span class="go"> 499               Hamilton Bank, NAEn Espanol         Miami  FL  24382   </span>
<span class="go"> 500                    Sinclair National Bank      Gravette  AR  34248   </span>
<span class="go"> 501                        Superior Bank, FSB      Hinsdale  IL  32646   </span>
<span class="go"> 502                       Malta National Bank         Malta  OH   6629   </span>
<span class="go"> 503           First Alliance Bank &amp; Trust Co.    Manchester  NH  34264   </span>
<span class="go"> 504         National State Bank of Metropolis    Metropolis  IL   3815   </span>
<span class="go"> 505                          Bank of Honolulu      Honolulu  HI  21029   </span>
<span class="go"> </span>
<span class="go">                    Acquiring Institution       Closing Date       Updated Date  </span>
<span class="go"> 0                  North Shore Bank, FSB       May 31, 2013       May 31, 2013  </span>
<span class="go"> 1                     Western State Bank       May 14, 2013       May 20, 2013  </span>
<span class="go"> 2                           Synovus Bank       May 10, 2013       May 21, 2013  </span>
<span class="go"> 3                     Capital Bank, N.A.       May 10, 2013       May 14, 2013  </span>
<span class="go"> 4                    Hamilton State Bank     April 26, 2013       May 16, 2013  </span>
<span class="go"> 5       CertusBank, National Association     April 26, 2013       May 17, 2013  </span>
<span class="go"> 6          First Federal Bank of Florida     April 19, 2013       May 16, 2013  </span>
<span class="go"> ..                                   ...                ...                ...  </span>
<span class="go"> 499     Israel Discount Bank of New York   January 11, 2002       June 5, 2012  </span>
<span class="go"> 500                   Delta Trust &amp; Bank  September 7, 2001  February 10, 2004  </span>
<span class="go"> 501                Superior Federal, FSB      July 27, 2001       June 5, 2012  </span>
<span class="go"> 502                    North Valley Bank        May 3, 2001  November 18, 2002  </span>
<span class="go"> 503  Southern New Hampshire Bank &amp; Trust   February 2, 2001  February 18, 2003  </span>
<span class="go"> 504              Banterra Bank of Marion  December 14, 2000     March 17, 2005  </span>
<span class="go"> 505                   Bank of the Orient   October 13, 2000     March 17, 2005  </span>
<span class="go"> </span>
<span class="go"> [506 rows x 7 columns]]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-871">如果你愿意，你甚至可以传递<code class="docutils literal"><span class="pre">StringIO</span></code>的实例</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [241]: </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file_path</span><span class="p">,</span> <span class="s1">&apos;r&apos;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="gp">   .....:</span>     <span class="n">sio</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="gp">   .....:</span> 

<span class="gp">In [242]: </span><span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">sio</span><span class="p">)</span>

<span class="gp">In [243]: </span><span class="n">dfs</span>
<span class="gr">Out[243]: </span>
<span class="go">[                                    Bank Name          City  ST   CERT  \</span>
<span class="go"> 0    Banks of Wisconsin d/b/a Bank of Kenosha       Kenosha  WI  35386   </span>
<span class="go"> 1                        Central Arizona Bank    Scottsdale  AZ  34527   </span>
<span class="go"> 2                                Sunrise Bank      Valdosta  GA  58185   </span>
<span class="go"> 3                       Pisgah Community Bank     Asheville  NC  58701   </span>
<span class="go"> 4                         Douglas County Bank  Douglasville  GA  21649   </span>
<span class="go"> 5                                Parkway Bank        Lenoir  NC  57158   </span>
<span class="go"> 6                      Chipola Community Bank      Marianna  FL  58034   </span>
<span class="go"> ..                                        ...           ...  ..    ...   </span>
<span class="go"> 499               Hamilton Bank, NAEn Espanol         Miami  FL  24382   </span>
<span class="go"> 500                    Sinclair National Bank      Gravette  AR  34248   </span>
<span class="go"> 501                        Superior Bank, FSB      Hinsdale  IL  32646   </span>
<span class="go"> 502                       Malta National Bank         Malta  OH   6629   </span>
<span class="go"> 503           First Alliance Bank &amp; Trust Co.    Manchester  NH  34264   </span>
<span class="go"> 504         National State Bank of Metropolis    Metropolis  IL   3815   </span>
<span class="go"> 505                          Bank of Honolulu      Honolulu  HI  21029   </span>
<span class="go"> </span>
<span class="go">                    Acquiring Institution       Closing Date       Updated Date  </span>
<span class="go"> 0                  North Shore Bank, FSB       May 31, 2013       May 31, 2013  </span>
<span class="go"> 1                     Western State Bank       May 14, 2013       May 20, 2013  </span>
<span class="go"> 2                           Synovus Bank       May 10, 2013       May 21, 2013  </span>
<span class="go"> 3                     Capital Bank, N.A.       May 10, 2013       May 14, 2013  </span>
<span class="go"> 4                    Hamilton State Bank     April 26, 2013       May 16, 2013  </span>
<span class="go"> 5       CertusBank, National Association     April 26, 2013       May 17, 2013  </span>
<span class="go"> 6          First Federal Bank of Florida     April 19, 2013       May 16, 2013  </span>
<span class="go"> ..                                   ...                ...                ...  </span>
<span class="go"> 499     Israel Discount Bank of New York   January 11, 2002       June 5, 2012  </span>
<span class="go"> 500                   Delta Trust &amp; Bank  September 7, 2001  February 10, 2004  </span>
<span class="go"> 501                Superior Federal, FSB      July 27, 2001       June 5, 2012  </span>
<span class="go"> 502                    North Valley Bank        May 3, 2001  November 18, 2002  </span>
<span class="go"> 503  Southern New Hampshire Bank &amp; Trust   February 2, 2001  February 18, 2003  </span>
<span class="go"> 504              Banterra Bank of Marion  December 14, 2000     March 17, 2005  </span>
<span class="go"> 505                   Bank of the Orient   October 13, 2000     March 17, 2005  </span>
<span class="go"> </span>
<span class="go"> [506 rows x 7 columns]]</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-872">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-873">以下示例不是由IPython评估程序运行的，因为有这么多网络访问功能会减慢文档构建。</span><span class="yiyi-st" id="yiyi-874">如果您发现错误或无法运行的示例，请随时在<a class="reference external" href="http://www.github.com/pandas-dev/pandas/issues">pandas GitHub问题页面</a>上报告错误。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-875">读取网址并匹配包含特定文本的表</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">match</span> <span class="o">=</span> <span class="s1">&apos;Metcalf Bank&apos;</span>
<span class="n">df_list</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">match</span><span class="o">=</span><span class="n">match</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-876">指定标题行（默认情况下，<code class="docutils literal"><span class="pre">&lt;th&gt;</span></code>元素用于形成列索引）；如果指定，则从数据减去经解析的报头元素（<code class="docutils literal"><span class="pre">&lt;th&gt;</span></code></span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-877">指定索引列</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-878">指定要跳过的行数</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">skiprows</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-879">使用列表指定要跳过的行数（<code class="docutils literal"><span class="pre">xrange</span></code>（仅限Python 2））</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">skiprows</span><span class="o">=</span><span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-880">指定HTML属性</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfs1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;id&apos;</span><span class="p">:</span> <span class="s1">&apos;table&apos;</span><span class="p">})</span>
<span class="n">dfs2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;class&apos;</span><span class="p">:</span> <span class="s1">&apos;sortable&apos;</span><span class="p">})</span>
<span class="k">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array_equal</span><span class="p">(</span><span class="n">dfs1</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">dfs2</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>  <span class="c1"># Should be True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-881">指定应转换为NaN的值</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">na_values</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;No Acquirer&apos;</span><span class="p">])</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-882"><span class="versionmodified">版本0.19中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-883">指定是否保留默认的NaN值集</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">keep_default_na</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-884"><span class="versionmodified">版本0.19中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-885">指定列的转换器。</span><span class="yiyi-st" id="yiyi-886">这对于具有前导零的数字文本数据很有用。</span><span class="yiyi-st" id="yiyi-887">默认情况下，数值型的列转换为数字类型，前导零将丢失。</span><span class="yiyi-st" id="yiyi-888">为了避免这种情况，我们可以将这些列转换为字符串。</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">url_mcc</span> <span class="o">=</span> <span class="s1">&apos;https://en.wikipedia.org/wiki/Mobile_country_code&apos;</span>
<span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url_mcc</span><span class="p">,</span> <span class="n">match</span><span class="o">=</span><span class="s1">&apos;Telekom Albania&apos;</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">converters</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;MNC&apos;</span><span class="p">:</span>
<span class="nb">str</span><span class="p">})</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-889"><span class="versionmodified">版本0.19中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-890">使用上述的一些组合</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">match</span><span class="o">=</span><span class="s1">&apos;Metcalf Bank&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-891">读入pandas <code class="docutils literal"><span class="pre">to_html</span></code>输出（浮点精度有些损失）</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">to_html</span><span class="p">(</span><span class="n">float_format</span><span class="o">=</span><span class="s1">&apos;{0:.40g}&apos;</span><span class="o">.</span><span class="n">format</span><span class="p">)</span>
<span class="n">dfin</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-892">The <code class="docutils literal"><span class="pre">lxml</span></code> backend will raise an error on a failed parse if that is the only parser you provide (if you only have a single parser you can provide just a string, but it is considered good practice to pass a list with one string if, for example, the function expects a sequence of strings)</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="s1">&apos;Metcalf Bank&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">flavor</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;lxml&apos;</span><span class="p">])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-893">要么</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="s1">&apos;Metcalf Bank&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">flavor</span><span class="o">=</span><span class="s1">&apos;lxml&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-894">但是，如果您安装了bs4和html5lib，并通过<code class="docutils literal"><span class="pre">None</span></code>或<code class="docutils literal"><span class="pre">[&apos;lxml&apos;，</span> <span class="pre">&apos;bs4&apos;]</span> 那么解析将很可能成功。</code></span><span class="yiyi-st" id="yiyi-895">请注意，<em>一旦解析成功，函数将返回</em>。</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_html</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="s1">&apos;Metcalf Bank&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">flavor</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;lxml&apos;</span><span class="p">,</span> <span class="s1">&apos;bs4&apos;</span><span class="p">])</span>
</pre></div>
</div>
</div>
<div class="section" id="writing-to-html-files">
<span id="io-html"></span><h3><span class="yiyi-st" id="yiyi-896">Writing to HTML files</span></h3>
<p><span class="yiyi-st" id="yiyi-897"><code class="docutils literal"><span class="pre">DataFrame</span></code>对象具有实例方法<code class="docutils literal"><span class="pre">to_html</span></code>，它将<code class="docutils literal"><span class="pre">DataFrame</span></code>的内容呈现为HTML表。</span><span class="yiyi-st" id="yiyi-898">函数参数与上述方法<code class="docutils literal"><span class="pre">to_string</span></code>中的相同。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-899">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-900">为了简洁起见，此处并未显示<code class="docutils literal"><span class="pre">DataFrame.to_html</span></code>的所有可能选项。</span><span class="yiyi-st" id="yiyi-901">有关完整的选项集，请参见<code class="xref py py-func docutils literal"><span class="pre">to_html()</span></code>。</span></p>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [244]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>

<span class="gp">In [245]: </span><span class="n">df</span>
<span class="gr">Out[245]: </span>
<span class="go">          0         1</span>
<span class="go">0 -0.184744  0.496971</span>
<span class="go">1 -0.856240  1.857977</span>

<span class="gp">In [246]: </span><span class="k">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">to_html</span><span class="p">())</span>  <span class="c1"># raw html</span>
<span class="go">&lt;table border=&quot;1&quot; class=&quot;dataframe&quot;&gt;</span>
<span class="go">  &lt;thead&gt;</span>
<span class="go">    &lt;tr style=&quot;text-align: right;&quot;&gt;</span>
<span class="go">      &lt;th&gt;&lt;/th&gt;</span>
<span class="go">      &lt;th&gt;0&lt;/th&gt;</span>
<span class="go">      &lt;th&gt;1&lt;/th&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">  &lt;/thead&gt;</span>
<span class="go">  &lt;tbody&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;th&gt;0&lt;/th&gt;</span>
<span class="go">      &lt;td&gt;-0.184744&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;0.496971&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;th&gt;1&lt;/th&gt;</span>
<span class="go">      &lt;td&gt;-0.856240&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;1.857977&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">  &lt;/tbody&gt;</span>
<span class="go">&lt;/table&gt;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-902">HTML：</span></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th><span class="yiyi-st" id="yiyi-903">0</span></th>
<th><span class="yiyi-st" id="yiyi-904">1</span></th>
</tr>
</thead>
<tbody>
<tr>
<th><span class="yiyi-st" id="yiyi-905">0</span></th>
<td><span class="yiyi-st" id="yiyi-906">-0.184744</span></td>
<td><span class="yiyi-st" id="yiyi-907">0.496971</span></td>
</tr>
<tr>
<th><span class="yiyi-st" id="yiyi-908">1</span></th>
<td><span class="yiyi-st" id="yiyi-909">-0.856240</span></td>
<td><span class="yiyi-st" id="yiyi-910">1.857977</span></td>
</tr>
</tbody>
</table><p><span class="yiyi-st" id="yiyi-911"><code class="docutils literal"><span class="pre">columns</span></code>参数将限制显示的列</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [247]: </span><span class="k">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">to_html</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>
<span class="go">&lt;table border=&quot;1&quot; class=&quot;dataframe&quot;&gt;</span>
<span class="go">  &lt;thead&gt;</span>
<span class="go">    &lt;tr style=&quot;text-align: right;&quot;&gt;</span>
<span class="go">      &lt;th&gt;&lt;/th&gt;</span>
<span class="go">      &lt;th&gt;0&lt;/th&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">  &lt;/thead&gt;</span>
<span class="go">  &lt;tbody&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;th&gt;0&lt;/th&gt;</span>
<span class="go">      &lt;td&gt;-0.184744&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;th&gt;1&lt;/th&gt;</span>
<span class="go">      &lt;td&gt;-0.856240&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">  &lt;/tbody&gt;</span>
<span class="go">&lt;/table&gt;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-912">HTML：</span></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th><span class="yiyi-st" id="yiyi-913">0</span></th>
</tr>
</thead>
<tbody>
<tr>
<th><span class="yiyi-st" id="yiyi-914">0</span></th>
<td><span class="yiyi-st" id="yiyi-915">-0.184744</span></td>
</tr>
<tr>
<th><span class="yiyi-st" id="yiyi-916">1</span></th>
<td><span class="yiyi-st" id="yiyi-917">-0.856240</span></td>
</tr>
</tbody>
</table><p><span class="yiyi-st" id="yiyi-918"><code class="docutils literal"><span class="pre">float_format</span></code>使用Python可调用方法来控制浮点值的精度</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [248]: </span><span class="k">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">to_html</span><span class="p">(</span><span class="n">float_format</span><span class="o">=</span><span class="s1">&apos;{0:.10f}&apos;</span><span class="o">.</span><span class="n">format</span><span class="p">))</span>
<span class="go">&lt;table border=&quot;1&quot; class=&quot;dataframe&quot;&gt;</span>
<span class="go">  &lt;thead&gt;</span>
<span class="go">    &lt;tr style=&quot;text-align: right;&quot;&gt;</span>
<span class="go">      &lt;th&gt;&lt;/th&gt;</span>
<span class="go">      &lt;th&gt;0&lt;/th&gt;</span>
<span class="go">      &lt;th&gt;1&lt;/th&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">  &lt;/thead&gt;</span>
<span class="go">  &lt;tbody&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;th&gt;0&lt;/th&gt;</span>
<span class="go">      &lt;td&gt;-0.1847438576&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;0.4969711327&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;th&gt;1&lt;/th&gt;</span>
<span class="go">      &lt;td&gt;-0.8562396763&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;1.8579766508&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">  &lt;/tbody&gt;</span>
<span class="go">&lt;/table&gt;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-919">HTML：</span></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th><span class="yiyi-st" id="yiyi-920">0</span></th>
<th><span class="yiyi-st" id="yiyi-921">1</span></th>
</tr>
</thead>
<tbody>
<tr>
<th><span class="yiyi-st" id="yiyi-922">0</span></th>
<td><span class="yiyi-st" id="yiyi-923">-0.1847438576</span></td>
<td><span class="yiyi-st" id="yiyi-924">0.4969711327</span></td>
</tr>
<tr>
<th><span class="yiyi-st" id="yiyi-925">1</span></th>
<td><span class="yiyi-st" id="yiyi-926">-0.8562396763</span></td>
<td><span class="yiyi-st" id="yiyi-927">1.8579766508</span></td>
</tr>
</tbody>
</table><p><span class="yiyi-st" id="yiyi-928"><code class="docutils literal"><span class="pre">bold_rows</span></code>会使行标签默认为粗体，但您可以关闭它</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [249]: </span><span class="k">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">to_html</span><span class="p">(</span><span class="n">bold_rows</span><span class="o">=</span><span class="bp">False</span><span class="p">))</span>
<span class="go">&lt;table border=&quot;1&quot; class=&quot;dataframe&quot;&gt;</span>
<span class="go">  &lt;thead&gt;</span>
<span class="go">    &lt;tr style=&quot;text-align: right;&quot;&gt;</span>
<span class="go">      &lt;th&gt;&lt;/th&gt;</span>
<span class="go">      &lt;th&gt;0&lt;/th&gt;</span>
<span class="go">      &lt;th&gt;1&lt;/th&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">  &lt;/thead&gt;</span>
<span class="go">  &lt;tbody&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;td&gt;0&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;-0.184744&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;0.496971&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;td&gt;1&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;-0.856240&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;1.857977&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">  &lt;/tbody&gt;</span>
<span class="go">&lt;/table&gt;</span>
</pre></div>
</div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th><span class="yiyi-st" id="yiyi-929">0</span></th>
<th><span class="yiyi-st" id="yiyi-930">1</span></th>
</tr>
</thead>
<tbody>
<tr>
<td><span class="yiyi-st" id="yiyi-931">0</span></td>
<td><span class="yiyi-st" id="yiyi-932">-0.184744</span></td>
<td><span class="yiyi-st" id="yiyi-933">0.496971</span></td>
</tr>
<tr>
<td><span class="yiyi-st" id="yiyi-934">1</span></td>
<td><span class="yiyi-st" id="yiyi-935">-0.856240</span></td>
<td><span class="yiyi-st" id="yiyi-936">1.857977</span></td>
</tr>
</tbody>
</table><p><span class="yiyi-st" id="yiyi-937"><code class="docutils literal"><span class="pre">classes</span></code>参数提供了给出生成的HTML表CSS类的能力。</span><span class="yiyi-st" id="yiyi-938">请注意，这些类是<em>附加</em>到现有的<code class="docutils literal"><span class="pre">&apos;dataframe&apos;</span></code>类。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [250]: </span><span class="k">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">to_html</span><span class="p">(</span><span class="n">classes</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;awesome_table_class&apos;</span><span class="p">,</span> <span class="s1">&apos;even_more_awesome_class&apos;</span><span class="p">]))</span>
<span class="go">&lt;table border=&quot;1&quot; class=&quot;dataframe awesome_table_class even_more_awesome_class&quot;&gt;</span>
<span class="go">  &lt;thead&gt;</span>
<span class="go">    &lt;tr style=&quot;text-align: right;&quot;&gt;</span>
<span class="go">      &lt;th&gt;&lt;/th&gt;</span>
<span class="go">      &lt;th&gt;0&lt;/th&gt;</span>
<span class="go">      &lt;th&gt;1&lt;/th&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">  &lt;/thead&gt;</span>
<span class="go">  &lt;tbody&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;th&gt;0&lt;/th&gt;</span>
<span class="go">      &lt;td&gt;-0.184744&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;0.496971&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;th&gt;1&lt;/th&gt;</span>
<span class="go">      &lt;td&gt;-0.856240&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;1.857977&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">  &lt;/tbody&gt;</span>
<span class="go">&lt;/table&gt;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-939">最后，<code class="docutils literal"><span class="pre">escape</span></code>参数允许您控制在生成的HTML中转义的“”和“＆”字符（默认为<code class="docutils literal"><span class="pre">True</span></code>）。</span><span class="yiyi-st" id="yiyi-940">因此，要获取没有转义字符的HTML，请传递<code class="docutils literal"><span class="pre">escape=False</span></code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [251]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span><span class="p">:</span> <span class="nb">list</span><span class="p">(</span><span class="s1">&apos;&amp;&lt;&gt;&apos;</span><span class="p">),</span> <span class="s1">&apos;b&apos;</span><span class="p">:</span> <span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">)})</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-941">转义：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [252]: </span><span class="k">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">to_html</span><span class="p">())</span>
<span class="go">&lt;table border=&quot;1&quot; class=&quot;dataframe&quot;&gt;</span>
<span class="go">  &lt;thead&gt;</span>
<span class="go">    &lt;tr style=&quot;text-align: right;&quot;&gt;</span>
<span class="go">      &lt;th&gt;&lt;/th&gt;</span>
<span class="go">      &lt;th&gt;a&lt;/th&gt;</span>
<span class="go">      &lt;th&gt;b&lt;/th&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">  &lt;/thead&gt;</span>
<span class="go">  &lt;tbody&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;th&gt;0&lt;/th&gt;</span>
<span class="go">      &lt;td&gt;&amp;amp;&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;-0.474063&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;th&gt;1&lt;/th&gt;</span>
<span class="go">      &lt;td&gt;&amp;lt;&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;-0.230305&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;th&gt;2&lt;/th&gt;</span>
<span class="go">      &lt;td&gt;&amp;gt;&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;-0.400654&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">  &lt;/tbody&gt;</span>
<span class="go">&lt;/table&gt;</span>
</pre></div>
</div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th><span class="yiyi-st" id="yiyi-942">一个</span></th>
<th><span class="yiyi-st" id="yiyi-943">b</span></th>
</tr>
</thead>
<tbody>
<tr>
<th><span class="yiyi-st" id="yiyi-944">0</span></th>
<td><span class="yiyi-st" id="yiyi-945">＆lt；</span></td>
<td><span class="yiyi-st" id="yiyi-946">-0.474063</span></td>
</tr>
<tr>
<th><span class="yiyi-st" id="yiyi-947">1</span></th>
<td><span class="yiyi-st" id="yiyi-948"></span></td>
<td><span class="yiyi-st" id="yiyi-949">-0.230305</span></td>
</tr>
<tr>
<th><span class="yiyi-st" id="yiyi-950">2</span></th>
<td><span class="yiyi-st" id="yiyi-951">&gt;</span></td>
<td><span class="yiyi-st" id="yiyi-952">-0.400654</span></td>
</tr>
</tbody>
</table><p><span class="yiyi-st" id="yiyi-953">未逸出：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [253]: </span><span class="k">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">to_html</span><span class="p">(</span><span class="n">escape</span><span class="o">=</span><span class="bp">False</span><span class="p">))</span>
<span class="go">&lt;table border=&quot;1&quot; class=&quot;dataframe&quot;&gt;</span>
<span class="go">  &lt;thead&gt;</span>
<span class="go">    &lt;tr style=&quot;text-align: right;&quot;&gt;</span>
<span class="go">      &lt;th&gt;&lt;/th&gt;</span>
<span class="go">      &lt;th&gt;a&lt;/th&gt;</span>
<span class="go">      &lt;th&gt;b&lt;/th&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">  &lt;/thead&gt;</span>
<span class="go">  &lt;tbody&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;th&gt;0&lt;/th&gt;</span>
<span class="go">      &lt;td&gt;&amp;&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;-0.474063&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;th&gt;1&lt;/th&gt;</span>
<span class="go">      &lt;td&gt;&lt;&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;-0.230305&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">    &lt;tr&gt;</span>
<span class="go">      &lt;th&gt;2&lt;/th&gt;</span>
<span class="go">      &lt;td&gt;&gt;&lt;/td&gt;</span>
<span class="go">      &lt;td&gt;-0.400654&lt;/td&gt;</span>
<span class="go">    &lt;/tr&gt;</span>
<span class="go">  &lt;/tbody&gt;</span>
<span class="go">&lt;/table&gt;</span>
</pre></div>
</div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th><span class="yiyi-st" id="yiyi-954">一个</span></th>
<th><span class="yiyi-st" id="yiyi-955">b</span></th>
</tr>
</thead>
<tbody>
<tr>
<th><span class="yiyi-st" id="yiyi-956">0</span></th>
<td><span class="yiyi-st" id="yiyi-957">＆lt；</span></td>
<td><span class="yiyi-st" id="yiyi-958">-0.474063</span></td>
</tr>
<tr>
<th><span class="yiyi-st" id="yiyi-959">1</span></th>
<td>
</td><td><span class="yiyi-st" id="yiyi-960">-0.230305</span></td>
</tr>
<tr>
<th><span class="yiyi-st" id="yiyi-961">2</span></th>
<td><span class="yiyi-st" id="yiyi-962">&gt;</span></td>
<td><span class="yiyi-st" id="yiyi-963">-0.400654</span></td>
</tr>
</tbody>
</table><div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-964">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-965">某些浏览器可能不会在前两个HTML表格的呈现方面显示不同。</span></p>
</div>
</div>
</div>
<div class="section" id="excel-files">
<span id="io-excel"></span><h2><span class="yiyi-st" id="yiyi-966">Excel files</span></h2>
<p><span class="yiyi-st" id="yiyi-967">The <a class="reference internal" href="generated/pandas.read_excel.html#pandas.read_excel" title="pandas.read_excel"><code class="xref py py-func docutils literal"><span class="pre">read_excel()</span></code></a> method can read Excel 2003 (<code class="docutils literal"><span class="pre">.xls</span></code>) and Excel 2007+ (<code class="docutils literal"><span class="pre">.xlsx</span></code>) files using the <code class="docutils literal"><span class="pre">xlrd</span></code> Python module. </span><span class="yiyi-st" id="yiyi-968"><a class="reference internal" href="generated/pandas.DataFrame.to_excel.html#pandas.DataFrame.to_excel" title="pandas.DataFrame.to_excel"><code class="xref py py-meth docutils literal"><span class="pre">to_excel()</span></code></a>实例方法用于将<code class="docutils literal"><span class="pre">DataFrame</span></code>保存到Excel。</span><span class="yiyi-st" id="yiyi-969">通常，语义与使用<a class="reference internal" href="#io-read-csv-table"><span class="std std-ref">csv</span></a>数据类似。</span><span class="yiyi-st" id="yiyi-970">有关某些高级策略，请参阅<a class="reference internal" href="cookbook.html#cookbook-excel"><span class="std std-ref">cookbook</span></a></span></p>
<div class="section" id="reading-excel-files">
<span id="io-excel-reader"></span><h3><span class="yiyi-st" id="yiyi-971">Reading Excel Files</span></h3>
<p><span class="yiyi-st" id="yiyi-972">在最基本的用例中，<code class="docutils literal"><span class="pre">read_excel</span></code>获取Excel文件的路径，<code class="docutils literal"><span class="pre">sheetname</span></code>指示要解析的工作表。</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># Returns a DataFrame</span>
<span class="n">read_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls&apos;</span><span class="p">,</span> <span class="n">sheetname</span><span class="o">=</span><span class="s1">&apos;Sheet1&apos;</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="excelfile-class">
<span id="io-excel-excelfile-class"></span><h4><span class="yiyi-st" id="yiyi-973"><code class="docutils literal"><span class="pre">ExcelFile</span></code> class</span></h4>
<p><span class="yiyi-st" id="yiyi-974">为了方便使用同一文件中的多个工作表，可以使用<code class="docutils literal"><span class="pre">ExcelFile</span></code>类包装文件，并将其传递到<code class="docutils literal"><span class="pre">read_excel</span></code>多张纸作为文件只读入内存一次。</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">xlsx</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">ExcelFile</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls)</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="n">xlsx</span><span class="p">,</span> <span class="s1">&apos;Sheet1&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-975"><code class="docutils literal"><span class="pre">ExcelFile</span></code>类也可以用作上下文管理器。</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">pd</span><span class="o">.</span><span class="n">ExcelFile</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls&apos;</span><span class="p">)</span> <span class="k">as</span> <span class="n">xls</span><span class="p">:</span>
    <span class="n">df1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="n">xls</span><span class="p">,</span> <span class="s1">&apos;Sheet1&apos;</span><span class="p">)</span>
    <span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="n">xls</span><span class="p">,</span> <span class="s1">&apos;Sheet2&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-976"><code class="docutils literal"><span class="pre">sheet_names</span></code>属性将生成文件中的工作表名称列表。</span></p>
<p><span class="yiyi-st" id="yiyi-977"><code class="docutils literal"><span class="pre">ExcelFile</span></code>的主要用例是解析具有不同参数的多个工作表</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">data</span> <span class="o">=</span> <span class="p">{}</span>
<span class="c1"># For when Sheet1&apos;s format differs from Sheet2</span>
<span class="k">with</span> <span class="n">pd</span><span class="o">.</span><span class="n">ExcelFile</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls&apos;</span><span class="p">)</span> <span class="k">as</span> <span class="n">xls</span><span class="p">:</span>
    <span class="n">data</span><span class="p">[</span><span class="s1">&apos;Sheet1&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="n">xls</span><span class="p">,</span> <span class="s1">&apos;Sheet1&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">na_values</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;NA&apos;</span><span class="p">])</span>
    <span class="n">data</span><span class="p">[</span><span class="s1">&apos;Sheet2&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="n">xls</span><span class="p">,</span> <span class="s1">&apos;Sheet2&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-978">请注意，如果对所有工作表使用相同的解析参数，则可以将工作表名称列表传递到<code class="docutils literal"><span class="pre">read_excel</span></code>，而不会降低性能。</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># using the ExcelFile class</span>
<span class="n">data</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">with</span> <span class="n">pd</span><span class="o">.</span><span class="n">ExcelFile</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls&apos;</span><span class="p">)</span> <span class="k">as</span> <span class="n">xls</span><span class="p">:</span>
    <span class="n">data</span><span class="p">[</span><span class="s1">&apos;Sheet1&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">read_excel</span><span class="p">(</span><span class="n">xls</span><span class="p">,</span> <span class="s1">&apos;Sheet1&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">na_values</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;NA&apos;</span><span class="p">])</span>
    <span class="n">data</span><span class="p">[</span><span class="s1">&apos;Sheet2&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">read_excel</span><span class="p">(</span><span class="n">xls</span><span class="p">,</span> <span class="s1">&apos;Sheet2&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">na_values</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;NA&apos;</span><span class="p">])</span>

<span class="c1"># equivalent using the read_excel function</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">read_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls&apos;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&apos;Sheet1&apos;</span><span class="p">,</span> <span class="s1">&apos;Sheet2&apos;</span><span class="p">],</span> <span class="n">index_col</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">na_values</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;NA&apos;</span><span class="p">])</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-979"><span class="versionmodified">版本0.12中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-980"><code class="docutils literal"><span class="pre">ExcelFile</span></code>已移至顶层命名空间。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-981"><span class="versionmodified">版本0.17中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-982"><code class="docutils literal"><span class="pre">read_excel</span></code>可以使用<code class="docutils literal"><span class="pre">ExcelFile</span></code>对象作为输入</span></p>
</div>
<div class="section" id="specifying-sheets">
<span id="io-excel-specifying-sheets"></span><h4><span class="yiyi-st" id="yiyi-983">Specifying Sheets</span></h4>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-984">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-985">第二个参数是<code class="docutils literal"><span class="pre">sheetname</span></code>，不要与<code class="docutils literal"><span class="pre">ExcelFile.sheet_names</span></code>混淆</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-986">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-987">ExcelFile的属性<code class="docutils literal"><span class="pre">sheet_names</span></code>提供对工作表列表的访问。</span></p>
</div>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-988">参数<code class="docutils literal"><span class="pre">sheetname</span></code>允许指定要读取的工作表。</span></li>
<li><span class="yiyi-st" id="yiyi-989"><code class="docutils literal"><span class="pre">sheetname</span></code>的默认值为0，表示要读取第一张工作表</span></li>
<li><span class="yiyi-st" id="yiyi-990">传递字符串以引用工作簿中特定工作表的名称。</span></li>
<li><span class="yiyi-st" id="yiyi-991">传递整数以引用表单的索引。</span><span class="yiyi-st" id="yiyi-992">索引遵循Python约定，从0开始。</span></li>
<li><span class="yiyi-st" id="yiyi-993">传递字符串或整数的列表，返回指定工作表的字典。</span></li>
<li><span class="yiyi-st" id="yiyi-994">传递<code class="docutils literal"><span class="pre">None</span></code>可返回所有可用工作表的字典。</span></li>
</ul>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># Returns a DataFrame</span>
<span class="n">read_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls&apos;</span><span class="p">,</span> <span class="s1">&apos;Sheet1&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">na_values</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;NA&apos;</span><span class="p">])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-995">使用表索引：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># Returns a DataFrame</span>
<span class="n">read_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls&apos;</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">na_values</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;NA&apos;</span><span class="p">])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-996">使用所有默认值：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># Returns a DataFrame</span>
<span class="n">read_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-997">使用无获取所有工作表：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># Returns a dictionary of DataFrames</span>
<span class="n">read_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls&apos;</span><span class="p">,</span><span class="n">sheetname</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-998">使用列表获取多个工作表：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># Returns the 1st and 4th sheet, as a dictionary of DataFrames.</span>
<span class="n">read_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls&apos;</span><span class="p">,</span><span class="n">sheetname</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;Sheet1&apos;</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-999"><span class="versionmodified">版本0.16中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1000"><code class="docutils literal"><span class="pre">read_excel</span></code>可以读取多个工作表，方法是将<code class="docutils literal"><span class="pre">sheetname</span></code>设置为工作表名称列表，工作表位置列表或<code class="docutils literal"><span class="pre">None</span></code>表示读取所有的表格。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1001"><span class="versionmodified">版本0.13中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1002">可以通过工作表索引或工作表名称分别使用整数或字符串指定工作表。</span></p>
</div>
<div class="section" id="reading-a-multiindex">
<span id="io-excel-reading-multiindex"></span><h4><span class="yiyi-st" id="yiyi-1003">Reading a <code class="docutils literal"><span class="pre">MultiIndex</span></code></span></h4>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1004"><span class="versionmodified">版本0.17中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1005"><code class="docutils literal"><span class="pre">read_excel</span></code> can read a <code class="docutils literal"><span class="pre">MultiIndex</span></code> index, by passing a list of columns to <code class="docutils literal"><span class="pre">index_col</span></code> and a <code class="docutils literal"><span class="pre">MultiIndex</span></code> column by passing a list of rows to <code class="docutils literal"><span class="pre">header</span></code>. </span><span class="yiyi-st" id="yiyi-1006">如果<code class="docutils literal"><span class="pre">index</span></code>或<code class="docutils literal"><span class="pre">columns</span></code>具有序列化的级别名称，那么通过指定组成级别的行/列，也将读入。</span></p>
<p><span class="yiyi-st" id="yiyi-1007">例如，要读取不带名称的<code class="docutils literal"><span class="pre">MultiIndex</span></code>索引：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [254]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span><span class="p">:[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span> <span class="s1">&apos;b&apos;</span><span class="p">:[</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="p">]},</span>
<span class="gp">   .....:</span>                   <span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_product</span><span class="p">([[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span><span class="s1">&apos;b&apos;</span><span class="p">],[</span><span class="s1">&apos;c&apos;</span><span class="p">,</span><span class="s1">&apos;d&apos;</span><span class="p">]]))</span>
<span class="gp">   .....:</span> 

<span class="gp">In [255]: </span><span class="n">df</span><span class="o">.</span><span class="n">to_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xlsx&apos;</span><span class="p">)</span>

<span class="gp">In [256]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xlsx&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">])</span>

<span class="gp">In [257]: </span><span class="n">df</span>
<span class="gr">Out[257]: </span>
<span class="go">     a  b</span>
<span class="go">a c  1  5</span>
<span class="go">  d  2  6</span>
<span class="go">b c  3  7</span>
<span class="go">  d  4  8</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1008">如果索引具有级别名称，则它们将使用相同的参数进行解析。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [258]: </span><span class="n">df</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">set_names</span><span class="p">([</span><span class="s1">&apos;lvl1&apos;</span><span class="p">,</span> <span class="s1">&apos;lvl2&apos;</span><span class="p">])</span>

<span class="gp">In [259]: </span><span class="n">df</span><span class="o">.</span><span class="n">to_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xlsx&apos;</span><span class="p">)</span>

<span class="gp">In [260]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xlsx&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">])</span>

<span class="gp">In [261]: </span><span class="n">df</span>
<span class="gr">Out[261]: </span>
<span class="go">           a  b</span>
<span class="go">lvl1 lvl2      </span>
<span class="go">a    c     1  5</span>
<span class="go">     d     2  6</span>
<span class="go">b    c     3  7</span>
<span class="go">     d     4  8</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1009">如果源文件同时具有<code class="docutils literal"><span class="pre">MultiIndex</span></code>索引和列，则指定每个列的列表应传递到<code class="docutils literal"><span class="pre">index_col</span></code>和<code class="docutils literal"><span class="pre">header</span></code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [262]: </span><span class="n">df</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_product</span><span class="p">([[</span><span class="s1">&apos;a&apos;</span><span class="p">],[</span><span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">]],</span> <span class="n">names</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;c1&apos;</span><span class="p">,</span> <span class="s1">&apos;c2&apos;</span><span class="p">])</span>

<span class="gp">In [263]: </span><span class="n">df</span><span class="o">.</span><span class="n">to_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xlsx&apos;</span><span class="p">)</span>

<span class="gp">In [264]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xlsx&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                     <span class="n">index_col</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">],</span> <span class="n">header</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [265]: </span><span class="n">df</span>
<span class="gr">Out[265]: </span>
<span class="go">c1         a   </span>
<span class="go">c2         b  d</span>
<span class="go">lvl1 lvl2      </span>
<span class="go">a    c     1  5</span>
<span class="go">     d     2  6</span>
<span class="go">b    c     3  7</span>
<span class="go">     d     4  8</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1010">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1011">保存在版本0.16.2或之前的具有索引名称的Excel文件仍然可以读入，但<code class="docutils literal"><span class="pre">has_index_names</span></code>参数必须指定为<code class="docutils literal"><span class="pre">True</span></code>。</span></p>
</div>
</div>
<div class="section" id="parsing-specific-columns">
<h4><span class="yiyi-st" id="yiyi-1012">Parsing Specific Columns</span></h4>
<p><span class="yiyi-st" id="yiyi-1013">通常情况下，用户将插入列以在Excel中进行临时计算，并且您可能不想读取这些列。</span><span class="yiyi-st" id="yiyi-1014"><cite>read_excel</cite>使用<cite>parse_cols</cite>关键字，以允许您指定要解析的列子集。</span></p>
<p><span class="yiyi-st" id="yiyi-1015">如果<cite>parse_cols</cite>是整数，则假定指示要解析的最后一列。</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">read_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls&apos;</span><span class="p">,</span> <span class="s1">&apos;Sheet1&apos;</span><span class="p">,</span> <span class="n">parse_cols</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1016">如果<cite>parse_cols</cite>是整数列表，则假定它是要解析的文件列索引。</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">read_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls&apos;</span><span class="p">,</span> <span class="s1">&apos;Sheet1&apos;</span><span class="p">,</span> <span class="n">parse_cols</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
</pre></div>
</div>
</div>
<div class="section" id="cell-converters">
<h4><span class="yiyi-st" id="yiyi-1017">Cell Converters</span></h4>
<p><span class="yiyi-st" id="yiyi-1018">可以通过<cite>转换器</cite>选项转换Excel单元格的内容。</span><span class="yiyi-st" id="yiyi-1019">例如，要将列转换为布尔值：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">read_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls&apos;</span><span class="p">,</span> <span class="s1">&apos;Sheet1&apos;</span><span class="p">,</span> <span class="n">converters</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;MyBools&apos;</span><span class="p">:</span> <span class="nb">bool</span><span class="p">})</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1020">此选项处理缺少的值，并将转换器中的异常视为缺失数据。</span><span class="yiyi-st" id="yiyi-1021">转换是逐个单元格应用的，而不是作为一个整体应用到列，因此不能保证数组dtype。</span><span class="yiyi-st" id="yiyi-1022">例如，具有缺失值的整数列不能转换为具有整数dtype的数组，因为NaN严格来说是一个浮点数。</span><span class="yiyi-st" id="yiyi-1023">您可以手动掩蔽缺少的数据以恢复整数dtype：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">cfun</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">if</span> <span class="n">x</span> <span class="k">else</span> <span class="o">-</span><span class="mi">1</span>
<span class="n">read_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xls&apos;</span><span class="p">,</span> <span class="s1">&apos;Sheet1&apos;</span><span class="p">,</span> <span class="n">converters</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;MyInts&apos;</span><span class="p">:</span> <span class="n">cfun</span><span class="p">})</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="writing-excel-files">
<span id="io-excel-writer"></span><h3><span class="yiyi-st" id="yiyi-1024">Writing Excel Files</span></h3>
<div class="section" id="writing-excel-files-to-disk">
<h4><span class="yiyi-st" id="yiyi-1025">Writing Excel Files to Disk</span></h4>
<p><span class="yiyi-st" id="yiyi-1026">要将DataFrame对象写入Excel文件的工作表，可以使用<code class="docutils literal"><span class="pre">to_excel</span></code>实例方法。</span><span class="yiyi-st" id="yiyi-1027">参数与上述的<code class="docutils literal"><span class="pre">to_csv</span></code>基本相同，第一个参数是excel文件的名称，第二个参数是可选的第二个参数应该写入DataFrame的工作表的名称。</span><span class="yiyi-st" id="yiyi-1028">例如：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">to_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xlsx&apos;</span><span class="p">,</span> <span class="n">sheet_name</span><span class="o">=</span><span class="s1">&apos;Sheet1&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1029">Files with a <code class="docutils literal"><span class="pre">.xls</span></code> extension will be written using <code class="docutils literal"><span class="pre">xlwt</span></code> and those with a <code class="docutils literal"><span class="pre">.xlsx</span></code> extension will be written using <code class="docutils literal"><span class="pre">xlsxwriter</span></code> (if available) or <code class="docutils literal"><span class="pre">openpyxl</span></code>.</span></p>
<p><span class="yiyi-st" id="yiyi-1030">DataFrame将以尝试模仿REPL输出的方式写入。</span><span class="yiyi-st" id="yiyi-1031">与0.12.0的一个区别是，<code class="docutils literal"><span class="pre">index_label</span></code>将放置在第二行，而不是第一行。</span><span class="yiyi-st" id="yiyi-1032">您可以通过将<code class="docutils literal"><span class="pre">to_excel()</span></code>中的<code class="docutils literal"><span class="pre">merge_cells</span></code>选项设置为<code class="docutils literal"><span class="pre">False</span></code>来获取先前的行为：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">to_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xlsx&apos;</span><span class="p">,</span> <span class="n">index_label</span><span class="o">=</span><span class="s1">&apos;label&apos;</span><span class="p">,</span> <span class="n">merge_cells</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1033">Panel类还有一个<code class="docutils literal"><span class="pre">to_excel</span></code>实例方法，它将Panel中的每个DataFrame写入单独的工作表。</span></p>
<p><span class="yiyi-st" id="yiyi-1034">为了在单个Excel文件中将单独的DataFrames写入单独的工作表，可以传递<code class="xref py py-class docutils literal"><span class="pre">ExcelWriter</span></code>。</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">ExcelWriter</span><span class="p">(</span><span class="s1">&apos;path_to_file.xlsx&apos;</span><span class="p">)</span> <span class="k">as</span> <span class="n">writer</span><span class="p">:</span>
    <span class="n">df1</span><span class="o">.</span><span class="n">to_excel</span><span class="p">(</span><span class="n">writer</span><span class="p">,</span> <span class="n">sheet_name</span><span class="o">=</span><span class="s1">&apos;Sheet1&apos;</span><span class="p">)</span>
    <span class="n">df2</span><span class="o">.</span><span class="n">to_excel</span><span class="p">(</span><span class="n">writer</span><span class="p">,</span> <span class="n">sheet_name</span><span class="o">=</span><span class="s1">&apos;Sheet2&apos;</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1035">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1036">在<code class="docutils literal"><span class="pre">read_excel</span></code>在内部，Excel将所有数字数据存储为浮点数。</span><span class="yiyi-st" id="yiyi-1037">因为这可能在读取数据时产生意想不到的行为，如果pandas不丢失信息，则默认为试图将整数转换为浮点数（<code class="docutils literal"><span class="pre">1.0</span> <span class="pre"> - &gt; <span class="pre">1</span></span></code>）。</span><span class="yiyi-st" id="yiyi-1038">您可以传递<code class="docutils literal"><span class="pre">convert_float=False</span></code>以禁用此行为，这可能会略微提高性能。</span></p>
</div>
</div>
<div class="section" id="writing-excel-files-to-memory">
<span id="io-excel-writing-buffer"></span><h4><span class="yiyi-st" id="yiyi-1039">Writing Excel Files to Memory</span></h4>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1040"><span class="versionmodified">版本0.17中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1041">Pandas支持使用<code class="xref py py-class docutils literal"><span class="pre">ExcelWriter</span></code>将Excel文件写入缓冲区对象，例如<code class="docutils literal"><span class="pre">StringIO</span></code>或<code class="docutils literal"><span class="pre">BytesIO</span></code>。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1042"><span class="versionmodified">版本0.17中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1043">添加了对Openpyxl&gt; = 2.2的支持</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># Safe import for either Python 2.x or 3.x</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="n">BytesIO</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">cStringIO</span> <span class="kn">import</span> <span class="n">StringIO</span> <span class="k">as</span> <span class="n">BytesIO</span>

<span class="n">bio</span> <span class="o">=</span> <span class="n">BytesIO</span><span class="p">()</span>

<span class="c1"># By setting the &apos;engine&apos; in the ExcelWriter constructor.</span>
<span class="n">writer</span> <span class="o">=</span> <span class="n">ExcelWriter</span><span class="p">(</span><span class="n">bio</span><span class="p">,</span> <span class="n">engine</span><span class="o">=</span><span class="s1">&apos;xlsxwriter&apos;</span><span class="p">)</span>
<span class="n">df</span><span class="o">.</span><span class="n">to_excel</span><span class="p">(</span><span class="n">writer</span><span class="p">,</span> <span class="n">sheet_name</span><span class="o">=</span><span class="s1">&apos;Sheet1&apos;</span><span class="p">)</span>

<span class="c1"># Save the workbook</span>
<span class="n">writer</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>

<span class="c1"># Seek to the beginning and read to copy the workbook to a variable in memory</span>
<span class="n">bio</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">workbook</span> <span class="o">=</span> <span class="n">bio</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1044">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1045"><code class="docutils literal"><span class="pre">engine</span></code>是可选的，但建议使用。</span><span class="yiyi-st" id="yiyi-1046">设置引擎将确定生成的工作簿的版本。</span><span class="yiyi-st" id="yiyi-1047">设置<code class="docutils literal"><span class="pre">engine=&apos;xlrd&apos;</span></code>将生成Excel 2003格式的工作簿（xls）。</span><span class="yiyi-st" id="yiyi-1048">使用<code class="docutils literal"><span class="pre">&apos;openpyxl&apos;</span></code>或<code class="docutils literal"><span class="pre">&apos;xlsxwriter&apos;</span></code>将生成Excel 2007格式的工作簿（xlsx）。</span><span class="yiyi-st" id="yiyi-1049">如果省略，则会生成Excel 2007格式的工作簿。</span></p>
</div>
</div>
</div>
<div class="section" id="excel-writer-engines">
<span id="io-excel-writers"></span><h3><span class="yiyi-st" id="yiyi-1050">Excel writer engines</span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1051"><span class="versionmodified">版本0.13中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1052"><code class="docutils literal"><span class="pre">pandas</span></code>通过两种方法选择Excel写程序：</span></p>
<ol class="arabic simple">
<li><span class="yiyi-st" id="yiyi-1053"><code class="docutils literal"><span class="pre">engine</span></code>关键字参数</span></li>
<li><span class="yiyi-st" id="yiyi-1054">文件扩展名（通过config选项中指定的默认值）</span></li>
</ol>
<p><span class="yiyi-st" id="yiyi-1055">默认情况下，<code class="docutils literal"><span class="pre">pandas</span></code>使用<a class="reference external" href="https://xlsxwriter.readthedocs.io">XlsxWriter</a>表示<code class="docutils literal"><span class="pre">.xlsx</span></code>和<a class="reference external" href="https://openpyxl.readthedocs.io/">openpyxl</a>表示<code class="docutils literal"><span class="pre">.xlsm</span></code>文件和<a class="reference external" href="http://www.python-excel.org">xlwt</a>用于<code class="docutils literal"><span class="pre">.xls</span></code>文件。</span><span class="yiyi-st" id="yiyi-1056">如果您安装了多个引擎，可以通过<a class="reference internal" href="options.html#options"><span class="std std-ref">setting the config options</span></a> <code class="docutils literal"><span class="pre">io.excel.xlsx.writer</span></code>和<code class="docutils literal"><span class="pre">io.excel.xls.writer</span></code>。</span><span class="yiyi-st" id="yiyi-1057">对于<code class="docutils literal"><span class="pre">.xlsx</span></code>文件，如果<a class="reference external" href="https://xlsxwriter.readthedocs.io">Xlsxwriter</a>不可用，pandas将回退到<a class="reference external" href="https://openpyxl.readthedocs.io/">openpyxl</a>。</span></p>
<p><span class="yiyi-st" id="yiyi-1058">要指定要使用的写入程序，可以将引擎关键字参数传递到<code class="docutils literal"><span class="pre">to_excel</span></code>和<code class="docutils literal"><span class="pre">ExcelWriter</span></code>。</span><span class="yiyi-st" id="yiyi-1059">内置引擎是：</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-1060"><code class="docutils literal"><span class="pre">openpyxl</span></code>：这包括从1.6.1稳定支持Openpyxl。</span><span class="yiyi-st" id="yiyi-1061">但是，建议使用版本2.2和更高版本，尤其是在使用样式时。</span></li>
<li><span class="yiyi-st" id="yiyi-1062"><code class="docutils literal"><span class="pre">xlsxwriter</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1063"><code class="docutils literal"><span class="pre">xlwt</span></code></span></li>
</ul>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># By setting the &apos;engine&apos; in the DataFrame and Panel &apos;to_excel()&apos; methods.</span>
<span class="n">df</span><span class="o">.</span><span class="n">to_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xlsx&apos;</span><span class="p">,</span> <span class="n">sheet_name</span><span class="o">=</span><span class="s1">&apos;Sheet1&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="o">=</span><span class="s1">&apos;xlsxwriter&apos;</span><span class="p">)</span>

<span class="c1"># By setting the &apos;engine&apos; in the ExcelWriter constructor.</span>
<span class="n">writer</span> <span class="o">=</span> <span class="n">ExcelWriter</span><span class="p">(</span><span class="s1">&apos;path_to_file.xlsx&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="o">=</span><span class="s1">&apos;xlsxwriter&apos;</span><span class="p">)</span>

<span class="c1"># Or via pandas configuration.</span>
<span class="kn">from</span> <span class="nn">pandas</span> <span class="kn">import</span> <span class="n">options</span>
<span class="n">options</span><span class="o">.</span><span class="n">io</span><span class="o">.</span><span class="n">excel</span><span class="o">.</span><span class="n">xlsx</span><span class="o">.</span><span class="n">writer</span> <span class="o">=</span> <span class="s1">&apos;xlsxwriter&apos;</span>

<span class="n">df</span><span class="o">.</span><span class="n">to_excel</span><span class="p">(</span><span class="s1">&apos;path_to_file.xlsx&apos;</span><span class="p">,</span> <span class="n">sheet_name</span><span class="o">=</span><span class="s1">&apos;Sheet1&apos;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="clipboard">
<span id="io-clipboard"></span><h2><span class="yiyi-st" id="yiyi-1064">Clipboard</span></h2>
<p><span class="yiyi-st" id="yiyi-1065">抓取数据的一种方法是使用<code class="docutils literal"><span class="pre">read_clipboard</span></code>方法，它将剪贴板缓冲区的内容传递给<code class="docutils literal"><span class="pre">read_table</span></code>方法。</span><span class="yiyi-st" id="yiyi-1066">例如，您可以将以下文本复制到剪贴板（在许多操作系统上为CTRL-C）：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span>  <span class="n">A</span> <span class="n">B</span> <span class="n">C</span>
<span class="n">x</span> <span class="mi">1</span> <span class="mi">4</span> <span class="n">p</span>
<span class="n">y</span> <span class="mi">2</span> <span class="mi">5</span> <span class="n">q</span>
<span class="n">z</span> <span class="mi">3</span> <span class="mi">6</span> <span class="n">r</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1067">然后通过调用将数据直接导入DataFrame：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">clipdf</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_clipboard</span><span class="p">()</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [266]: </span><span class="n">clipdf</span>
<span class="gr">Out[266]: </span>
<span class="go">   A  B  C</span>
<span class="go">x  1  4  p</span>
<span class="go">y  2  5  q</span>
<span class="go">z  3  6  r</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1068"><code class="docutils literal"><span class="pre">to_clipboard</span></code>方法可用于将DataFrame的内容写入剪贴板。</span><span class="yiyi-st" id="yiyi-1069">然后，您可以将剪贴板内容粘贴到其他应用程序（在许多操作系统上的CTRL-V）。</span><span class="yiyi-st" id="yiyi-1070">这里我们说明将DataFrame写入剪贴板并读回。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [267]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span>

<span class="gp">In [268]: </span><span class="n">df</span>
<span class="gr">Out[268]: </span>
<span class="go">          0         1         2</span>
<span class="go">0 -0.288267 -0.084905  0.004772</span>
<span class="go">1  1.382989  0.343635 -1.253994</span>
<span class="go">2 -0.124925  0.212244  0.496654</span>
<span class="go">3  0.525417  1.238640 -1.210543</span>
<span class="go">4 -1.175743 -0.172372 -0.734129</span>

<span class="gp">In [269]: </span><span class="n">df</span><span class="o">.</span><span class="n">to_clipboard</span><span class="p">()</span>

<span class="gp">In [270]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_clipboard</span><span class="p">()</span>
<span class="gr">Out[270]: </span>
<span class="go">          0         1         2</span>
<span class="go">0 -0.288267 -0.084905  0.004772</span>
<span class="go">1  1.382989  0.343635 -1.253994</span>
<span class="go">2 -0.124925  0.212244  0.496654</span>
<span class="go">3  0.525417  1.238640 -1.210543</span>
<span class="go">4 -1.175743 -0.172372 -0.734129</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1071">我们可以看到，我们得到了相同的内容，我们以前写到剪贴板。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1072">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1073">您可能需要在Linux上安装xclip或xsel（使用gtk或PyQt4模块）来使用这些方法。</span></p>
</div>
</div>
<div class="section" id="pickling">
<span id="io-pickle"></span><h2><span class="yiyi-st" id="yiyi-1074">Pickling</span></h2>
<p><span class="yiyi-st" id="yiyi-1075">所有的pandas对象都配备了使用Python的<code class="docutils literal"><span class="pre">cPickle</span></code>模块使用pickle格式将数据结构保存到磁盘的<code class="docutils literal"><span class="pre">to_pickle</span></code>方法。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [271]: </span><span class="n">df</span>
<span class="gr">Out[271]: </span>
<span class="go">          0         1         2</span>
<span class="go">0 -0.288267 -0.084905  0.004772</span>
<span class="go">1  1.382989  0.343635 -1.253994</span>
<span class="go">2 -0.124925  0.212244  0.496654</span>
<span class="go">3  0.525417  1.238640 -1.210543</span>
<span class="go">4 -1.175743 -0.172372 -0.734129</span>

<span class="gp">In [272]: </span><span class="n">df</span><span class="o">.</span><span class="n">to_pickle</span><span class="p">(</span><span class="s1">&apos;foo.pkl&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1076"><code class="docutils literal"><span class="pre">pandas</span></code>命名空间中的<code class="docutils literal"><span class="pre">read_pickle</span></code>函数可用于从文件加载任何腌制的pandas对象（或任何其他腌制对象）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [273]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_pickle</span><span class="p">(</span><span class="s1">&apos;foo.pkl&apos;</span><span class="p">)</span>
<span class="gr">Out[273]: </span>
<span class="go">          0         1         2</span>
<span class="go">0 -0.288267 -0.084905  0.004772</span>
<span class="go">1  1.382989  0.343635 -1.253994</span>
<span class="go">2 -0.124925  0.212244  0.496654</span>
<span class="go">3  0.525417  1.238640 -1.210543</span>
<span class="go">4 -1.175743 -0.172372 -0.734129</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1077">警告</span></p>
<p><span class="yiyi-st" id="yiyi-1078">加载从不受信任的来源接收的pickled数据可能是不安全的。</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1079">请参阅：<a class="reference external" href="http://docs.python.org/2.7/library/pickle.html">http://docs.python.org/2.7/library/pickle.html</a></span></p>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1080">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1081">几个内部重构，0.13（<a class="reference internal" href="whatsnew.html#whatsnew-0130-refactoring"><span class="std std-ref">Series Refactoring</span></a>）和0.15（<a class="reference internal" href="whatsnew.html#whatsnew-0150-refactoring"><span class="std std-ref">Index Refactoring</span></a>）保留了与这些版本之前创建的pickles的兼容性。</span><span class="yiyi-st" id="yiyi-1082">但是，这些必须用<code class="docutils literal"><span class="pre">pd.read_pickle</span></code>读取，而不是默认的python <code class="docutils literal"><span class="pre">pickle.load</span></code>。</span><span class="yiyi-st" id="yiyi-1083">有关详细说明，请参阅<a class="reference external" href="http://stackoverflow.com/questions/20444593/pandas-compiled-from-source-default-pickle-behavior-changed">此问题</a>。</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1084">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1085">这些方法以前是<code class="docutils literal"><span class="pre">pd.save</span></code>和<code class="docutils literal"><span class="pre">pd.load</span></code>，在0.12.0之前，现在已被弃用。</span></p>
</div>
</div>
<div class="section" id="msgpack-experimental">
<span id="io-msgpack"></span><h2><span class="yiyi-st" id="yiyi-1086">msgpack (experimental)</span></h2>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1087"><span class="versionmodified">版本0.13.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1088">从0.13.0开始，pandas支持对象序列化的<code class="docutils literal"><span class="pre">msgpack</span></code>格式。</span><span class="yiyi-st" id="yiyi-1089">这是一种轻量级的便携式二进制格式，类似于二进制JSON，高度节省空间，并且在写入（串行化）和读取（反序列化）时提供良好的性能。</span></p>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1090">警告</span></p>
<p><span class="yiyi-st" id="yiyi-1091">这是熊猫的一个非常新的特点。</span><span class="yiyi-st" id="yiyi-1092">我们打算在<code class="docutils literal"><span class="pre">msgpack</span></code>数据的io中提供某些优化。</span><span class="yiyi-st" id="yiyi-1093">由于这被标记为实验库，所以存储格式在未来版本之前可能不稳定。</span></p>
<p><span class="yiyi-st" id="yiyi-1094">由于写入格式更改和其他问题：</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="48%">
<col width="52%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-1095">包装</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-1096">可以解压缩</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-1097">pre-0.17 / Python 2</span></td>
<td><span class="yiyi-st" id="yiyi-1098">任何</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-1099">pre-0.17 / Python 3</span></td>
<td><span class="yiyi-st" id="yiyi-1100">任何</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-1101">0.17 / Python 2</span></td>
<td><ul class="first last simple">
<li><span class="yiyi-st" id="yiyi-1102">0.17 / Python 2</span></li>
<li><span class="yiyi-st" id="yiyi-1103">&gt; = 0.18 /任何Python</span></li>
</ul>
</td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-1104">0.17 / Python 3</span></td>
<td><span class="yiyi-st" id="yiyi-1105">&gt; = 0.18 /任何Python</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-1106">0.18</span></td>
<td><span class="yiyi-st" id="yiyi-1107">&gt; = 0.18</span></td>
</tr>
</tbody>
</table>
<p class="last"><span class="yiyi-st" id="yiyi-1108">阅读（旧版本打包的文件）是向后兼容的，除了在Python 2中用0.17打包的文件，在这种情况下只能在Python 2中解压缩。</span></p>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [274]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span><span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;AB&apos;</span><span class="p">))</span>

<span class="gp">In [275]: </span><span class="n">df</span><span class="o">.</span><span class="n">to_msgpack</span><span class="p">(</span><span class="s1">&apos;foo.msg&apos;</span><span class="p">)</span>

<span class="gp">In [276]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_msgpack</span><span class="p">(</span><span class="s1">&apos;foo.msg&apos;</span><span class="p">)</span>
<span class="gr">Out[276]: </span>
<span class="go">          A         B</span>
<span class="go">0  0.154336  0.710999</span>
<span class="go">1  0.398096  0.765220</span>
<span class="go">2  0.586749  0.293052</span>
<span class="go">3  0.290293  0.710783</span>
<span class="go">4  0.988593  0.062106</span>

<span class="gp">In [277]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span><span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">,</span><span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">))</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1109">你可以传递一个对象的列表，你会收到他们反序列化。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [278]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_msgpack</span><span class="p">(</span><span class="s1">&apos;foo.msg&apos;</span><span class="p">,</span> <span class="n">df</span><span class="p">,</span> <span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]),</span> <span class="n">s</span><span class="p">)</span>

<span class="gp">In [279]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_msgpack</span><span class="p">(</span><span class="s1">&apos;foo.msg&apos;</span><span class="p">)</span>
<span class="gr">Out[279]: </span>
<span class="go">[          A         B</span>
<span class="go"> 0  0.154336  0.710999</span>
<span class="go"> 1  0.398096  0.765220</span>
<span class="go"> 2  0.586749  0.293052</span>
<span class="go"> 3  0.290293  0.710783</span>
<span class="go"> 4  0.988593  0.062106, &apos;foo&apos;, array([1, 2, 3]), 2013-01-01    0.690810</span>
<span class="go"> 2013-01-02    0.235907</span>
<span class="go"> 2013-01-03    0.712756</span>
<span class="go"> 2013-01-04    0.119599</span>
<span class="go"> 2013-01-05    0.023493</span>
<span class="go"> Freq: D, dtype: float64]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1110">您可以传递<code class="docutils literal"><span class="pre">iterator=True</span></code>来迭代解压缩的结果</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [280]: </span><span class="k">for</span> <span class="n">o</span> <span class="ow">in</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_msgpack</span><span class="p">(</span><span class="s1">&apos;foo.msg&apos;</span><span class="p">,</span><span class="n">iterator</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
<span class="gp">   .....:</span>     <span class="k">print</span> <span class="n">o</span>
<span class="gp">   .....:</span> 
<span class="go">          A         B</span>
<span class="go">0  0.154336  0.710999</span>
<span class="go">1  0.398096  0.765220</span>
<span class="go">2  0.586749  0.293052</span>
<span class="go">3  0.290293  0.710783</span>
<span class="go">4  0.988593  0.062106</span>
<span class="go">foo</span>
<span class="go">[1 2 3]</span>
<span class="go">2013-01-01    0.690810</span>
<span class="go">2013-01-02    0.235907</span>
<span class="go">2013-01-03    0.712756</span>
<span class="go">2013-01-04    0.119599</span>
<span class="go">2013-01-05    0.023493</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1111">您可以将<code class="docutils literal"><span class="pre">append=True</span></code>传递给编写器，以附加到现有包</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [281]: </span><span class="n">df</span><span class="o">.</span><span class="n">to_msgpack</span><span class="p">(</span><span class="s1">&apos;foo.msg&apos;</span><span class="p">,</span><span class="n">append</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [282]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_msgpack</span><span class="p">(</span><span class="s1">&apos;foo.msg&apos;</span><span class="p">)</span>
<span class="gr">Out[282]: </span>
<span class="go">[          A         B</span>
<span class="go"> 0  0.154336  0.710999</span>
<span class="go"> 1  0.398096  0.765220</span>
<span class="go"> 2  0.586749  0.293052</span>
<span class="go"> 3  0.290293  0.710783</span>
<span class="go"> 4  0.988593  0.062106, &apos;foo&apos;, array([1, 2, 3]), 2013-01-01    0.690810</span>
<span class="go"> 2013-01-02    0.235907</span>
<span class="go"> 2013-01-03    0.712756</span>
<span class="go"> 2013-01-04    0.119599</span>
<span class="go"> 2013-01-05    0.023493</span>
<span class="go"> Freq: D, dtype: float64,           A         B</span>
<span class="go"> 0  0.154336  0.710999</span>
<span class="go"> 1  0.398096  0.765220</span>
<span class="go"> 2  0.586749  0.293052</span>
<span class="go"> 3  0.290293  0.710783</span>
<span class="go"> 4  0.988593  0.062106]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1112">与其他io方法不同，<code class="docutils literal"><span class="pre">to_msgpack</span></code>可用于每个对象基础上，<code class="docutils literal"><span class="pre">df.to_msgpack()</span></code>和使用顶层<code class="docutils literal"><span class="pre">pd.to_msgpack(...)</span></code>其中，您可以打包任意集合的python列表，dicts，标量，同时混合熊猫对象。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [283]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_msgpack</span><span class="p">(</span><span class="s1">&apos;foo2.msg&apos;</span><span class="p">,</span> <span class="p">{</span> <span class="s1">&apos;dict&apos;</span> <span class="p">:</span> <span class="p">[</span> <span class="p">{</span> <span class="s1">&apos;df&apos;</span> <span class="p">:</span> <span class="n">df</span> <span class="p">},</span> <span class="p">{</span> <span class="s1">&apos;string&apos;</span> <span class="p">:</span> <span class="s1">&apos;foo&apos;</span> <span class="p">},</span> <span class="p">{</span> <span class="s1">&apos;scalar&apos;</span> <span class="p">:</span> <span class="mf">1.</span> <span class="p">},</span> <span class="p">{</span> <span class="s1">&apos;s&apos;</span> <span class="p">:</span> <span class="n">s</span> <span class="p">}</span> <span class="p">]</span> <span class="p">})</span>

<span class="gp">In [284]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_msgpack</span><span class="p">(</span><span class="s1">&apos;foo2.msg&apos;</span><span class="p">)</span>
<span class="gr">Out[284]: </span>
<span class="go">{&apos;dict&apos;: ({&apos;df&apos;:           A         B</span>
<span class="go">   0  0.154336  0.710999</span>
<span class="go">   1  0.398096  0.765220</span>
<span class="go">   2  0.586749  0.293052</span>
<span class="go">   3  0.290293  0.710783</span>
<span class="go">   4  0.988593  0.062106},</span>
<span class="go">  {&apos;string&apos;: &apos;foo&apos;},</span>
<span class="go">  {&apos;scalar&apos;: 1.0},</span>
<span class="go">  {&apos;s&apos;: 2013-01-01    0.690810</span>
<span class="go">   2013-01-02    0.235907</span>
<span class="go">   2013-01-03    0.712756</span>
<span class="go">   2013-01-04    0.119599</span>
<span class="go">   2013-01-05    0.023493</span>
<span class="go">   Freq: D, dtype: float64})}</span>
</pre></div>
</div>
<div class="section" id="read-write-api">
<h3><span class="yiyi-st" id="yiyi-1113">Read/Write API</span></h3>
<p><span class="yiyi-st" id="yiyi-1114">Msgpacks也可以从字符串读取和写入字符串。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [285]: </span><span class="n">df</span><span class="o">.</span><span class="n">to_msgpack</span><span class="p">()</span>
<span class="gr">Out[285]: </span><span class="s1">&apos;</span><span class="se">\x84\xa6</span><span class="s1">blocks</span><span class="se">\x91\x86\xa5</span><span class="s1">dtype</span><span class="se">\xa7</span><span class="s1">float64</span><span class="se">\xa8</span><span class="s1">compress</span><span class="se">\xc0\xa4</span><span class="s1">locs</span><span class="se">\x86\xa4</span><span class="s1">ndim</span><span class="se">\x01\xa5</span><span class="s1">dtype</span><span class="se">\xa5</span><span class="s1">int64</span><span class="se">\xa8</span><span class="s1">compress</span><span class="se">\xc0\xa4</span><span class="s1">data</span><span class="se">\xd8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\xa5</span><span class="s1">shape</span><span class="se">\x91\x02\xa3</span><span class="s1">typ</span><span class="se">\xa7</span><span class="s1">ndarray</span><span class="se">\xa5</span><span class="s1">shape</span><span class="se">\x92\x02\x05\xa6</span><span class="s1">values</span><span class="se">\xc7</span><span class="s1">P</span><span class="se">\x00\xa0\xab\xfb</span><span class="s1">6H</span><span class="se">\xc1\xc3</span><span class="s1">?</span><span class="se">\x98</span><span class="s1">(oMgz</span><span class="se">\xd9</span><span class="s1">?</span><span class="se">\x17\xae</span><span class="s1">d</span><span class="se">\\\xa5\xc6\xe2</span><span class="s1">?</span><span class="se">\xdc\xd0\x1b</span><span class="s1">d(</span><span class="se">\x94\xd2</span><span class="s1">?</span><span class="se">\xb5\xe8\xf5\x0e\x8d\xa2\xef</span><span class="s1">?</span><span class="se">\x02</span><span class="s1">D</span><span class="se">\xeb</span><span class="s1">O</span><span class="se">\x80\xc0\xe6</span><span class="s1">?</span><span class="se">\x16\xbd</span><span class="s1">dQ</span><span class="se">\xae</span><span class="s1">|</span><span class="se">\xe8</span><span class="s1">?</span><span class="se">\x10</span><span class="s1">?Ya[</span><span class="se">\xc1\xd2</span><span class="s1">?</span><span class="se">\xa8\xfd\xcf\xa0\xbc\xbe\xe6</span><span class="s1">? Z</span><span class="se">\xe1\t</span><span class="s1">i</span><span class="se">\xcc\xaf</span><span class="s1">?</span><span class="se">\xa5</span><span class="s1">klass</span><span class="se">\xaa</span><span class="s1">FloatBlock</span><span class="se">\xa4</span><span class="s1">axes</span><span class="se">\x92\x86\xa4</span><span class="s1">name</span><span class="se">\xc0\xa5</span><span class="s1">dtype</span><span class="se">\xa6</span><span class="s1">object</span><span class="se">\xa8</span><span class="s1">compress</span><span class="se">\xc0\xa4</span><span class="s1">data</span><span class="se">\x92\xc4\x01</span><span class="s1">A</span><span class="se">\xc4\x01</span><span class="s1">B</span><span class="se">\xa5</span><span class="s1">klass</span><span class="se">\xa5</span><span class="s1">Index</span><span class="se">\xa3</span><span class="s1">typ</span><span class="se">\xa5</span><span class="s1">index</span><span class="se">\x86\xa4</span><span class="s1">name</span><span class="se">\xc0\xa4</span><span class="s1">stop</span><span class="se">\x05\xa5</span><span class="s1">start</span><span class="se">\x00\xa4</span><span class="s1">step</span><span class="se">\x01\xa5</span><span class="s1">klass</span><span class="se">\xaa</span><span class="s1">RangeIndex</span><span class="se">\xa3</span><span class="s1">typ</span><span class="se">\xab</span><span class="s1">range_index</span><span class="se">\xa3</span><span class="s1">typ</span><span class="se">\xad</span><span class="s1">block_manager</span><span class="se">\xa5</span><span class="s1">klass</span><span class="se">\xa9</span><span class="s1">DataFrame&apos;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1115">此外，您可以连接字符串以生成原始对象的列表。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [286]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_msgpack</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">to_msgpack</span><span class="p">()</span> <span class="o">+</span> <span class="n">s</span><span class="o">.</span><span class="n">to_msgpack</span><span class="p">())</span>
<span class="gr">Out[286]: </span>
<span class="go">[          A         B</span>
<span class="go"> 0  0.154336  0.710999</span>
<span class="go"> 1  0.398096  0.765220</span>
<span class="go"> 2  0.586749  0.293052</span>
<span class="go"> 3  0.290293  0.710783</span>
<span class="go"> 4  0.988593  0.062106, 2013-01-01    0.690810</span>
<span class="go"> 2013-01-02    0.235907</span>
<span class="go"> 2013-01-03    0.712756</span>
<span class="go"> 2013-01-04    0.119599</span>
<span class="go"> 2013-01-05    0.023493</span>
<span class="go"> Freq: D, dtype: float64]</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="hdf5-pytables">
<span id="io-hdf5"></span><h2><span class="yiyi-st" id="yiyi-1116">HDF5 (PyTables)</span></h2>
<p><span class="yiyi-st" id="yiyi-1117"><code class="docutils literal"><span class="pre">HDFStore</span></code>是一个类似dict的对象，使用优秀的<a class="reference external" href="http://www.pytables.org/">PyTables</a>库，使用HDF5格式能快速读取和写入pandas。</span><span class="yiyi-st" id="yiyi-1118">有关某些高级策略，请参阅<a class="reference internal" href="cookbook.html#cookbook-hdf"><span class="std std-ref">cookbook</span></a></span></p>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1119">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1120">从版本0.15.0开始，pandas需要<code class="docutils literal"><span class="pre">PyTables</span></code>&gt; = 3.0.0。</span><span class="yiyi-st" id="yiyi-1121">使用先前版本的pandas / <code class="docutils literal"><span class="pre">PyTables</span></code>&gt; = 2.3编写的存储是完全兼容的（这是以前的最小<code class="docutils literal"><span class="pre">PyTables</span></code>所需的版本）。</span></p>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1122">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1123">有一个<code class="docutils literal"><span class="pre">PyTables</span></code>索引错误，当使用索引查询存储时可能会出现。</span><span class="yiyi-st" id="yiyi-1124">如果您看到返回的结果子集，请升级到<code class="docutils literal"><span class="pre">PyTables</span></code>&gt; = 3.2。</span><span class="yiyi-st" id="yiyi-1125">以前创建的商店将需要使用更新的版本重写。</span></p>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1126">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1127">从版本0.17.0起，<code class="docutils literal"><span class="pre">HDFStore</span></code>在默认情况下不会删除具有所有缺失值的行。</span><span class="yiyi-st" id="yiyi-1128">以前，如果缺少所有值（索引除外），则<code class="docutils literal"><span class="pre">HDFStore</span></code>将不会将这些行写入磁盘。</span></p>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [287]: </span><span class="n">store</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">HDFStore</span><span class="p">(</span><span class="s1">&apos;store.h5&apos;</span><span class="p">)</span>

<span class="gp">In [288]: </span><span class="k">print</span><span class="p">(</span><span class="n">store</span><span class="p">)</span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: store.h5</span>
<span class="go">Empty</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1129">对象可以写入文件，就像将键值对添加到dict中一样：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [289]: </span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">1234</span><span class="p">)</span>

<span class="gp">In [290]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;1/1/2000&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">8</span><span class="p">)</span>

<span class="gp">In [291]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">,</span> <span class="s1">&apos;e&apos;</span><span class="p">])</span>

<span class="gp">In [292]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">,</span>
<span class="gp">   .....:</span>                   <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;C&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [293]: </span><span class="n">wp</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Panel</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="n">items</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;Item1&apos;</span><span class="p">,</span> <span class="s1">&apos;Item2&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>               <span class="n">major_axis</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;1/1/2000&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">),</span>
<span class="gp">   .....:</span>               <span class="n">minor_axis</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;C&apos;</span><span class="p">,</span> <span class="s1">&apos;D&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="c"># store.put(&apos;s&apos;, s) is an equivalent method</span>
<span class="gp">In [294]: </span><span class="n">store</span><span class="p">[</span><span class="s1">&apos;s&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">s</span>

<span class="gp">In [295]: </span><span class="n">store</span><span class="p">[</span><span class="s1">&apos;df&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">df</span>

<span class="gp">In [296]: </span><span class="n">store</span><span class="p">[</span><span class="s1">&apos;wp&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">wp</span>

<span class="c"># the type of stored data</span>
<span class="gp">In [297]: </span><span class="n">store</span><span class="o">.</span><span class="n">root</span><span class="o">.</span><span class="n">wp</span><span class="o">.</span><span class="n">_v_attrs</span><span class="o">.</span><span class="n">pandas_type</span>
<span class="gr">Out[297]: </span><span class="s1">&apos;wide&apos;</span>

<span class="gp">In [298]: </span><span class="n">store</span>
<span class="gr">Out[298]: </span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: store.h5</span>
<span class="go">/df            frame        (shape-&gt;[8,3])  </span>
<span class="go">/s             series       (shape-&gt;[5])    </span>
<span class="go">/wp            wide         (shape-&gt;[2,5,4])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1130">在当前或以后的Python会话中，您可以检索存储的对象：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># store.get(&apos;df&apos;) is an equivalent method</span>
<span class="gp">In [299]: </span><span class="n">store</span><span class="p">[</span><span class="s1">&apos;df&apos;</span><span class="p">]</span>
<span class="gr">Out[299]: </span>
<span class="go">                   A         B         C</span>
<span class="go">2000-01-01  0.887163  0.859588 -0.636524</span>
<span class="go">2000-01-02  0.015696 -2.242685  1.150036</span>
<span class="go">2000-01-03  0.991946  0.953324 -2.021255</span>
<span class="go">2000-01-04 -0.334077  0.002118  0.405453</span>
<span class="go">2000-01-05  0.289092  1.321158 -1.546906</span>
<span class="go">2000-01-06 -0.202646 -0.655969  0.193421</span>
<span class="go">2000-01-07  0.553439  1.318152 -0.469305</span>
<span class="go">2000-01-08  0.675554 -1.817027 -0.183109</span>

<span class="c"># dotted (attribute) access provides get as well</span>
<span class="gp">In [300]: </span><span class="n">store</span><span class="o">.</span><span class="n">df</span>
<span class="gr">Out[300]: </span>
<span class="go">                   A         B         C</span>
<span class="go">2000-01-01  0.887163  0.859588 -0.636524</span>
<span class="go">2000-01-02  0.015696 -2.242685  1.150036</span>
<span class="go">2000-01-03  0.991946  0.953324 -2.021255</span>
<span class="go">2000-01-04 -0.334077  0.002118  0.405453</span>
<span class="go">2000-01-05  0.289092  1.321158 -1.546906</span>
<span class="go">2000-01-06 -0.202646 -0.655969  0.193421</span>
<span class="go">2000-01-07  0.553439  1.318152 -0.469305</span>
<span class="go">2000-01-08  0.675554 -1.817027 -0.183109</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1131">删除由键指定的对象</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># store.remove(&apos;wp&apos;) is an equivalent method</span>
<span class="gp">In [301]: </span><span class="k">del</span> <span class="n">store</span><span class="p">[</span><span class="s1">&apos;wp&apos;</span><span class="p">]</span>

<span class="gp">In [302]: </span><span class="n">store</span>
<span class="gr">Out[302]: </span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: store.h5</span>
<span class="go">/df            frame        (shape-&gt;[8,3])</span>
<span class="go">/s             series       (shape-&gt;[5])  </span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1132">关闭store对象，上下文管理器</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [303]: </span><span class="n">store</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

<span class="gp">In [304]: </span><span class="n">store</span>
<span class="gr">Out[304]: </span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: store.h5</span>
<span class="go">File is CLOSED</span>

<span class="gp">In [305]: </span><span class="n">store</span><span class="o">.</span><span class="n">is_open</span>
<span class="gr">Out[305]: </span><span class="bp">False</span>

<span class="c"># Working with, and automatically closing the store with the context</span>
<span class="c"># manager</span>
<span class="gp">In [306]: </span><span class="k">with</span> <span class="n">pd</span><span class="o">.</span><span class="n">HDFStore</span><span class="p">(</span><span class="s1">&apos;store.h5&apos;</span><span class="p">)</span> <span class="k">as</span> <span class="n">store</span><span class="p">:</span>
<span class="gp">   .....:</span>     <span class="n">store</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="gp">   .....:</span> 
</pre></div>
</div>
<div class="section" id="id2">
<h3><span class="yiyi-st" id="yiyi-1133">Read/Write API</span></h3>
<p><span class="yiyi-st" id="yiyi-1134"><code class="docutils literal"><span class="pre">HDFStore</span></code> supports an top-level API using <code class="docutils literal"><span class="pre">read_hdf</span></code> for reading and <code class="docutils literal"><span class="pre">to_hdf</span></code> for writing, similar to how <code class="docutils literal"><span class="pre">read_csv</span></code> and <code class="docutils literal"><span class="pre">to_csv</span></code> work. </span><span class="yiyi-st" id="yiyi-1135">（新的0.11.0）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [307]: </span><span class="n">df_tl</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">A</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">)),</span> <span class="n">B</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">))))</span>

<span class="gp">In [308]: </span><span class="n">df_tl</span><span class="o">.</span><span class="n">to_hdf</span><span class="p">(</span><span class="s1">&apos;store_tl.h5&apos;</span><span class="p">,</span><span class="s1">&apos;table&apos;</span><span class="p">,</span><span class="n">append</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [309]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="s1">&apos;store_tl.h5&apos;</span><span class="p">,</span> <span class="s1">&apos;table&apos;</span><span class="p">,</span> <span class="n">where</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;index&gt;2&apos;</span><span class="p">])</span>
<span class="gr">Out[309]: </span>
<span class="go">   A  B</span>
<span class="go">3  3  3</span>
<span class="go">4  4  4</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1136">从0.17.0版本开始，HDFStore将不再删除默认情况下都缺失的行。</span><span class="yiyi-st" id="yiyi-1137">可以通过设置<code class="docutils literal"><span class="pre">dropna=True</span></code>来启用此行为。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [310]: </span><span class="n">df_with_missing</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;col1&apos;</span><span class="p">:[</span><span class="mi">0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
<span class="gp">   .....:</span>                                 <span class="s1">&apos;col2&apos;</span><span class="p">:[</span><span class="mi">1</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">]})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [311]: </span><span class="n">df_with_missing</span>
<span class="gr">Out[311]: </span>
<span class="go">   col1  col2</span>
<span class="go">0   0.0   1.0</span>
<span class="go">1   NaN   NaN</span>
<span class="go">2   2.0   NaN</span>

<span class="gp">In [312]: </span><span class="n">df_with_missing</span><span class="o">.</span><span class="n">to_hdf</span><span class="p">(</span><span class="s1">&apos;file.h5&apos;</span><span class="p">,</span> <span class="s1">&apos;df_with_missing&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                         <span class="n">format</span> <span class="o">=</span> <span class="s1">&apos;table&apos;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&apos;w&apos;</span><span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [313]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="s1">&apos;file.h5&apos;</span><span class="p">,</span> <span class="s1">&apos;df_with_missing&apos;</span><span class="p">)</span>
<span class="gr">Out[313]: </span>
<span class="go">   col1  col2</span>
<span class="go">0   0.0   1.0</span>
<span class="go">1   NaN   NaN</span>
<span class="go">2   2.0   NaN</span>

<span class="gp">In [314]: </span><span class="n">df_with_missing</span><span class="o">.</span><span class="n">to_hdf</span><span class="p">(</span><span class="s1">&apos;file.h5&apos;</span><span class="p">,</span> <span class="s1">&apos;df_with_missing&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                         <span class="n">format</span> <span class="o">=</span> <span class="s1">&apos;table&apos;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&apos;w&apos;</span><span class="p">,</span> <span class="n">dropna</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [315]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="s1">&apos;file.h5&apos;</span><span class="p">,</span> <span class="s1">&apos;df_with_missing&apos;</span><span class="p">)</span>
<span class="gr">Out[315]: </span>
<span class="go">   col1  col2</span>
<span class="go">0   0.0   1.0</span>
<span class="go">2   2.0   NaN</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1138">对于<code class="docutils literal"><span class="pre">Panel</span></code>的长轴也是如此：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [316]: </span><span class="n">matrix</span> <span class="o">=</span> <span class="p">[[[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">],[</span><span class="mi">1</span><span class="p">,</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">]],</span>
<span class="gp">   .....:</span>        <span class="p">[[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">],</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">]],</span>
<span class="gp">   .....:</span>        <span class="p">[[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">],[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">]]]</span>
<span class="gp">   .....:</span> 

<span class="gp">In [317]: </span><span class="n">panel_with_major_axis_all_missing</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Panel</span><span class="p">(</span><span class="n">matrix</span><span class="p">,</span>
<span class="gp">   .....:</span>         <span class="n">items</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;Item1&apos;</span><span class="p">,</span> <span class="s1">&apos;Item2&apos;</span><span class="p">,</span><span class="s1">&apos;Item3&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>         <span class="n">major_axis</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">],</span>
<span class="gp">   .....:</span>         <span class="n">minor_axis</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;C&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [318]: </span><span class="n">panel_with_major_axis_all_missing</span>
<span class="gr">Out[318]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 3 (items) x 2 (major_axis) x 3 (minor_axis)</span>
<span class="go">Items axis: Item1 to Item3</span>
<span class="go">Major_axis axis: 1 to 2</span>
<span class="go">Minor_axis axis: A to C</span>

<span class="gp">In [319]: </span><span class="n">panel_with_major_axis_all_missing</span><span class="o">.</span><span class="n">to_hdf</span><span class="p">(</span><span class="s1">&apos;file.h5&apos;</span><span class="p">,</span> <span class="s1">&apos;panel&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                                         <span class="n">dropna</span> <span class="o">=</span> <span class="bp">True</span><span class="p">,</span>
<span class="gp">   .....:</span>                                         <span class="n">format</span><span class="o">=</span><span class="s1">&apos;table&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                                         <span class="n">mode</span><span class="o">=</span><span class="s1">&apos;w&apos;</span><span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [320]: </span><span class="n">reloaded</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="s1">&apos;file.h5&apos;</span><span class="p">,</span> <span class="s1">&apos;panel&apos;</span><span class="p">)</span>

<span class="gp">In [321]: </span><span class="n">reloaded</span>
<span class="gr">Out[321]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 3 (items) x 1 (major_axis) x 3 (minor_axis)</span>
<span class="go">Items axis: Item1 to Item3</span>
<span class="go">Major_axis axis: 2 to 2</span>
<span class="go">Minor_axis axis: A to C</span>
</pre></div>
</div>
</div>
<div class="section" id="fixed-format">
<span id="io-hdf5-fixed"></span><h3><span class="yiyi-st" id="yiyi-1139">Fixed Format</span></h3>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1140">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1141">这是<code class="docutils literal"><span class="pre">Storer</span></code>格式之前的0.13.0。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1142">以上示例显示使用<code class="docutils literal"><span class="pre">put</span></code>进行存储，其以固定阵列格式（称为<code class="docutils literal"><span class="pre">fixed</span></code>格式）将HDF5写入<code class="docutils literal"><span class="pre">PyTables</span></code>。</span><span class="yiyi-st" id="yiyi-1143">这些类型的存储是<strong>不是</strong>可写的一次写入（虽然你可以简单地删除它们和重写）。</span><span class="yiyi-st" id="yiyi-1144">它们也不是<strong>可查询的</strong>；它们必须全部检索。</span><span class="yiyi-st" id="yiyi-1145">它们也不支持具有非唯一列名的数据帧。</span><span class="yiyi-st" id="yiyi-1146"><code class="docutils literal"><span class="pre">fixed</span></code>格式存储提供非常快的写入和稍微快于<code class="docutils literal"><span class="pre">table</span></code>存储的读取。</span><span class="yiyi-st" id="yiyi-1147">当使用<code class="docutils literal"><span class="pre">put</span></code>或<code class="docutils literal"><span class="pre">to_hdf</span></code>或<code class="docutils literal"><span class="pre">format=&apos;fixed&apos;</span></code>或<code class="docutils literal"><span class="pre">format=&apos;f&apos;</span></code></span></p>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1148">警告</span></p>
<p><span class="yiyi-st" id="yiyi-1149">如果您尝试使用<code class="docutils literal"><span class="pre">where</span></code>检索，则<code class="docutils literal"><span class="pre">fixed</span></code>格式将产生<code class="docutils literal"><span class="pre">TypeError</span></code>。</span></p>
<div class="last highlight-python"><div class="highlight"><pre><span></span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span><span class="o">.</span><span class="n">to_hdf</span><span class="p">(</span><span class="s1">&apos;test_fixed.h5&apos;</span><span class="p">,</span><span class="s1">&apos;df&apos;</span><span class="p">)</span>

<span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="s1">&apos;test_fixed.h5&apos;</span><span class="p">,</span><span class="s1">&apos;df&apos;</span><span class="p">,</span><span class="n">where</span><span class="o">=</span><span class="s1">&apos;index&gt;5&apos;</span><span class="p">)</span>
<span class="ne">TypeError</span><span class="p">:</span> <span class="n">cannot</span> <span class="k">pass</span> <span class="n">a</span> <span class="n">where</span> <span class="n">specification</span> <span class="n">when</span> <span class="n">reading</span> <span class="n">a</span> <span class="n">fixed</span> <span class="n">format</span><span class="o">.</span>
           <span class="n">this</span> <span class="n">store</span> <span class="n">must</span> <span class="n">be</span> <span class="n">selected</span> <span class="ow">in</span> <span class="n">its</span> <span class="n">entirety</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="table-format">
<span id="io-hdf5-table"></span><h3><span class="yiyi-st" id="yiyi-1150">Table Format</span></h3>
<p><span class="yiyi-st" id="yiyi-1151"><code class="docutils literal"><span class="pre">HDFStore</span></code>支持磁盘上的另一个<code class="docutils literal"><span class="pre">PyTables</span></code>格式，<code class="docutils literal"><span class="pre">table</span></code>格式。</span><span class="yiyi-st" id="yiyi-1152">概念上，<code class="docutils literal"><span class="pre">table</span></code>的形状非常像DataFrame，具有行和列。</span><span class="yiyi-st" id="yiyi-1153">可以在相同会话或其他会话中附加<code class="docutils literal"><span class="pre">table</span></code>。</span><span class="yiyi-st" id="yiyi-1154">此外，支持删除和查询类型操作。</span><span class="yiyi-st" id="yiyi-1155">This format is specified by <code class="docutils literal"><span class="pre">format=&apos;table&apos;</span></code> or <code class="docutils literal"><span class="pre">format=&apos;t&apos;</span></code> to <code class="docutils literal"><span class="pre">append</span></code> or <code class="docutils literal"><span class="pre">put</span></code> or <code class="docutils literal"><span class="pre">to_hdf</span></code></span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1156"><span class="versionmodified">版本0.13中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1157">此格式可以设置为默认情况下启用<code class="docutils literal"><span class="pre">put/append/to_hdf</span></code>的选项<code class="docutils literal"><span class="pre">pd.set_option(&apos;io.hdf.default_format&apos;,&apos;table&apos;)</span></code>以<code class="docutils literal"><span class="pre">table</span></code>格式保存。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [322]: </span><span class="n">store</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">HDFStore</span><span class="p">(</span><span class="s1">&apos;store.h5&apos;</span><span class="p">)</span>

<span class="gp">In [323]: </span><span class="n">df1</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span>

<span class="gp">In [324]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="mi">4</span><span class="p">:]</span>

<span class="c"># append data (creates a table automatically)</span>
<span class="gp">In [325]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">,</span> <span class="n">df1</span><span class="p">)</span>

<span class="gp">In [326]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">,</span> <span class="n">df2</span><span class="p">)</span>

<span class="gp">In [327]: </span><span class="n">store</span>
<span class="gr">Out[327]: </span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: store.h5</span>
<span class="go">/df            frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;3,indexers-&gt;[index])</span>

<span class="c"># select the entire object</span>
<span class="gp">In [328]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">)</span>
<span class="gr">Out[328]: </span>
<span class="go">                   A         B         C</span>
<span class="go">2000-01-01  0.887163  0.859588 -0.636524</span>
<span class="go">2000-01-02  0.015696 -2.242685  1.150036</span>
<span class="go">2000-01-03  0.991946  0.953324 -2.021255</span>
<span class="go">2000-01-04 -0.334077  0.002118  0.405453</span>
<span class="go">2000-01-05  0.289092  1.321158 -1.546906</span>
<span class="go">2000-01-06 -0.202646 -0.655969  0.193421</span>
<span class="go">2000-01-07  0.553439  1.318152 -0.469305</span>
<span class="go">2000-01-08  0.675554 -1.817027 -0.183109</span>

<span class="c"># the type of stored data</span>
<span class="gp">In [329]: </span><span class="n">store</span><span class="o">.</span><span class="n">root</span><span class="o">.</span><span class="n">df</span><span class="o">.</span><span class="n">_v_attrs</span><span class="o">.</span><span class="n">pandas_type</span>
<span class="gr">Out[329]: </span><span class="s1">&apos;frame_table&apos;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1158">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1159">您还可以通过将<code class="docutils literal"><span class="pre">format=&apos;table&apos;</span></code>或<code class="docutils literal"><span class="pre">format=&apos;t&apos;</span></code>传递到<code class="docutils literal"><span class="pre">put</span></code>来创建<code class="docutils literal"><span class="pre">table</span></code></span></p>
</div>
</div>
<div class="section" id="hierarchical-keys">
<span id="io-hdf5-keys"></span><h3><span class="yiyi-st" id="yiyi-1160">Hierarchical Keys</span></h3>
<p><span class="yiyi-st" id="yiyi-1161">商店的键可以指定为字符串。</span><span class="yiyi-st" id="yiyi-1162">这些可以是层次化路径名格式（例如<code class="docutils literal"><span class="pre">foo/bar/bah</span></code>），它将生成子存储的层次结构（或PyTables中的<code class="docutils literal"><span class="pre">Groups</span></code>） 。</span><span class="yiyi-st" id="yiyi-1163">键可以指定前面的&apos;/&apos;，并且是绝对的（例如&apos;foo&apos;是指&apos;/ foo&apos;）。</span><span class="yiyi-st" id="yiyi-1164">删除操作可以删除子存储和BELOW中的所有内容，因此请小心<em></em>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [330]: </span><span class="n">store</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="s1">&apos;foo/bar/bah&apos;</span><span class="p">,</span> <span class="n">df</span><span class="p">)</span>

<span class="gp">In [331]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;food/orange&apos;</span><span class="p">,</span> <span class="n">df</span><span class="p">)</span>

<span class="gp">In [332]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;food/apple&apos;</span><span class="p">,</span>  <span class="n">df</span><span class="p">)</span>

<span class="gp">In [333]: </span><span class="n">store</span>
<span class="gr">Out[333]: </span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: store.h5</span>
<span class="go">/df                     frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;3,indexers-&gt;[index])</span>
<span class="go">/foo/bar/bah            frame        (shape-&gt;[8,3])                                       </span>
<span class="go">/food/apple             frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;3,indexers-&gt;[index])</span>
<span class="go">/food/orange            frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;3,indexers-&gt;[index])</span>

<span class="c"># a list of keys are returned</span>
<span class="gp">In [334]: </span><span class="n">store</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="gr">Out[334]: </span><span class="p">[</span><span class="s1">&apos;/df&apos;</span><span class="p">,</span> <span class="s1">&apos;/food/apple&apos;</span><span class="p">,</span> <span class="s1">&apos;/food/orange&apos;</span><span class="p">,</span> <span class="s1">&apos;/foo/bar/bah&apos;</span><span class="p">]</span>

<span class="c"># remove all nodes under this level</span>
<span class="gp">In [335]: </span><span class="n">store</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s1">&apos;food&apos;</span><span class="p">)</span>

<span class="gp">In [336]: </span><span class="n">store</span>
<span class="gr">Out[336]: </span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: store.h5</span>
<span class="go">/df                     frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;3,indexers-&gt;[index])</span>
<span class="go">/foo/bar/bah            frame        (shape-&gt;[8,3])                                       </span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1165">警告</span></p>
<p><span class="yiyi-st" id="yiyi-1166">分层密钥不能被检索为如上所述的存储在根节点下的项目的点（属性）访问。</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mi">8</span><span class="p">]:</span> <span class="n">store</span><span class="o">.</span><span class="n">foo</span><span class="o">.</span><span class="n">bar</span><span class="o">.</span><span class="n">bah</span>
<span class="ne">AttributeError</span><span class="p">:</span> <span class="s1">&apos;HDFStore&apos;</span> <span class="nb">object</span> <span class="n">has</span> <span class="n">no</span> <span class="n">attribute</span> <span class="s1">&apos;foo&apos;</span>

<span class="c1"># you can directly access the actual PyTables node but using the root node</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">9</span><span class="p">]:</span> <span class="n">store</span><span class="o">.</span><span class="n">root</span><span class="o">.</span><span class="n">foo</span><span class="o">.</span><span class="n">bar</span><span class="o">.</span><span class="n">bah</span>
<span class="n">Out</span><span class="p">[</span><span class="mi">9</span><span class="p">]:</span>
<span class="o">/</span><span class="n">foo</span><span class="o">/</span><span class="n">bar</span><span class="o">/</span><span class="n">bah</span> <span class="p">(</span><span class="n">Group</span><span class="p">)</span> <span class="s1">&apos;&apos;</span>
  <span class="n">children</span> <span class="p">:</span><span class="o">=</span> <span class="p">[</span><span class="s1">&apos;block0_items&apos;</span> <span class="p">(</span><span class="n">Array</span><span class="p">),</span> <span class="s1">&apos;block0_values&apos;</span> <span class="p">(</span><span class="n">Array</span><span class="p">),</span> <span class="s1">&apos;axis0&apos;</span> <span class="p">(</span><span class="n">Array</span><span class="p">),</span> <span class="s1">&apos;axis1&apos;</span> <span class="p">(</span><span class="n">Array</span><span class="p">)]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1167">相反，使用显式字符串为基础的键</span></p>
<div class="last highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [337]: </span><span class="n">store</span><span class="p">[</span><span class="s1">&apos;foo/bar/bah&apos;</span><span class="p">]</span>
<span class="gr">Out[337]: </span>
<span class="go">                   A         B         C</span>
<span class="go">2000-01-01  0.887163  0.859588 -0.636524</span>
<span class="go">2000-01-02  0.015696 -2.242685  1.150036</span>
<span class="go">2000-01-03  0.991946  0.953324 -2.021255</span>
<span class="go">2000-01-04 -0.334077  0.002118  0.405453</span>
<span class="go">2000-01-05  0.289092  1.321158 -1.546906</span>
<span class="go">2000-01-06 -0.202646 -0.655969  0.193421</span>
<span class="go">2000-01-07  0.553439  1.318152 -0.469305</span>
<span class="go">2000-01-08  0.675554 -1.817027 -0.183109</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="storing-types">
<span id="io-hdf5-types"></span><h3><span class="yiyi-st" id="yiyi-1168">Storing Types</span></h3>
<div class="section" id="storing-mixed-types-in-a-table">
<h4><span class="yiyi-st" id="yiyi-1169">Storing Mixed Types in a Table</span></h4>
<p><span class="yiyi-st" id="yiyi-1170">支持存储混合数据类型。</span><span class="yiyi-st" id="yiyi-1171">字符串使用附加列的最大大小存储为固定宽度。</span><span class="yiyi-st" id="yiyi-1172">随后尝试追加更长的字符串将产生<code class="docutils literal"><span class="pre">ValueError</span></code>。</span></p>
<p><span class="yiyi-st" id="yiyi-1173">将<code class="docutils literal"><span class="pre">min_itemsize = {`values`：</span> <span class="pre">size}</span></code>作为参数传递将为字符串列设置较大的最小值。</span><span class="yiyi-st" id="yiyi-1174">存储<code class="docutils literal"><span class="pre">浮点，</span> <span class="pre">字符串，</span> <span class="pre">int，</span> <span class="pre">bools，</span> <span class="pre">datetime64</span> t0&gt;目前支持。</code></span><span class="yiyi-st" id="yiyi-1175">对于字符串列，传递<code class="docutils literal"><span class="pre">nan_rep</span> <span class="pre">=</span> <span class="pre">&apos;nan&apos;</span></code>可以更改磁盘上的默认nan表示转换为/从<cite>np.nan</cite>），默认为<cite>nan</cite>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [338]: </span><span class="n">df_mixed</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span> <span class="s1">&apos;A&apos;</span> <span class="p">:</span> <span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">),</span>
<span class="gp">   .....:</span>                           <span class="s1">&apos;B&apos;</span> <span class="p">:</span> <span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">),</span>
<span class="gp">   .....:</span>                           <span class="s1">&apos;C&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">),</span><span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;float32&apos;</span><span class="p">),</span>
<span class="gp">   .....:</span>                           <span class="s1">&apos;string&apos;</span> <span class="p">:</span><span class="s1">&apos;string&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                           <span class="s1">&apos;int&apos;</span> <span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="gp">   .....:</span>                           <span class="s1">&apos;bool&apos;</span> <span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="gp">   .....:</span>                           <span class="s1">&apos;datetime64&apos;</span> <span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;20010102&apos;</span><span class="p">)},</span>
<span class="gp">   .....:</span>                         <span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">8</span><span class="p">)))</span>
<span class="gp">   .....:</span> 

<span class="gp">In [339]: </span><span class="n">df_mixed</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">3</span><span class="p">:</span><span class="mi">5</span><span class="p">,[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;string&apos;</span><span class="p">,</span> <span class="s1">&apos;datetime64&apos;</span><span class="p">]]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>

<span class="gp">In [340]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;df_mixed&apos;</span><span class="p">,</span> <span class="n">df_mixed</span><span class="p">,</span> <span class="n">min_itemsize</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&apos;values&apos;</span><span class="p">:</span> <span class="mi">50</span><span class="p">})</span>

<span class="gp">In [341]: </span><span class="n">df_mixed1</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df_mixed&apos;</span><span class="p">)</span>

<span class="gp">In [342]: </span><span class="n">df_mixed1</span>
<span class="gr">Out[342]: </span>
<span class="go">          A         B         C  bool datetime64  int  string</span>
<span class="go">0  0.704721 -1.152659 -0.430096  True 2001-01-02    1  string</span>
<span class="go">1 -0.785435  0.631979  0.767369  True 2001-01-02    1  string</span>
<span class="go">2  0.462060  0.039513  0.984920  True 2001-01-02    1  string</span>
<span class="go">3       NaN       NaN  0.270836  True        NaT    1     NaN</span>
<span class="go">4       NaN       NaN  1.391986  True        NaT    1     NaN</span>
<span class="go">5       NaN       NaN  0.079842  True        NaT    1     NaN</span>
<span class="go">6  2.007843  0.152631 -0.399965  True 2001-01-02    1  string</span>
<span class="go">7  0.226963  0.164530 -1.027851  True 2001-01-02    1  string</span>

<span class="gp">In [343]: </span><span class="n">df_mixed1</span><span class="o">.</span><span class="n">get_dtype_counts</span><span class="p">()</span>
<span class="gr">Out[343]: </span>
<span class="go">bool              1</span>
<span class="go">datetime64[ns]    1</span>
<span class="go">float32           1</span>
<span class="go">float64           2</span>
<span class="go">int64             1</span>
<span class="go">object            1</span>
<span class="go">dtype: int64</span>

<span class="c"># we have provided a minimum string column size</span>
<span class="gp">In [344]: </span><span class="n">store</span><span class="o">.</span><span class="n">root</span><span class="o">.</span><span class="n">df_mixed</span><span class="o">.</span><span class="n">table</span>
<span class="gr">Out[344]: </span>
<span class="go">/df_mixed/table (Table(8,)) &apos;&apos;</span>
<span class="go">  description := {</span>
<span class="go">  &quot;index&quot;: Int64Col(shape=(), dflt=0, pos=0),</span>
<span class="go">  &quot;values_block_0&quot;: Float64Col(shape=(2,), dflt=0.0, pos=1),</span>
<span class="go">  &quot;values_block_1&quot;: Float32Col(shape=(1,), dflt=0.0, pos=2),</span>
<span class="go">  &quot;values_block_2&quot;: Int64Col(shape=(1,), dflt=0, pos=3),</span>
<span class="go">  &quot;values_block_3&quot;: Int64Col(shape=(1,), dflt=0, pos=4),</span>
<span class="go">  &quot;values_block_4&quot;: BoolCol(shape=(1,), dflt=False, pos=5),</span>
<span class="go">  &quot;values_block_5&quot;: StringCol(itemsize=50, shape=(1,), dflt=&apos;&apos;, pos=6)}</span>
<span class="go">  byteorder := &apos;little&apos;</span>
<span class="go">  chunkshape := (689,)</span>
<span class="go">  autoindex := True</span>
<span class="go">  colindexes := {</span>
<span class="go">    &quot;index&quot;: Index(6, medium, shuffle, zlib(1)).is_csi=False}</span>
</pre></div>
</div>
</div>
<div class="section" id="storing-multi-index-dataframes">
<h4><span class="yiyi-st" id="yiyi-1176">Storing Multi-Index DataFrames</span></h4>
<p><span class="yiyi-st" id="yiyi-1177">将多索引数据帧存储为表非常类似于从同构索引DataFrames存储/选择。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [345]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="p">(</span><span class="n">levels</span><span class="o">=</span><span class="p">[[</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;bar&apos;</span><span class="p">,</span> <span class="s1">&apos;baz&apos;</span><span class="p">,</span> <span class="s1">&apos;qux&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                               <span class="p">[</span><span class="s1">&apos;one&apos;</span><span class="p">,</span> <span class="s1">&apos;two&apos;</span><span class="p">,</span> <span class="s1">&apos;three&apos;</span><span class="p">]],</span>
<span class="gp">   .....:</span>                       <span class="n">labels</span><span class="o">=</span><span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span>
<span class="gp">   .....:</span>                               <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]],</span>
<span class="gp">   .....:</span>                       <span class="n">names</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;bar&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [346]: </span><span class="n">df_mi</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">,</span>
<span class="gp">   .....:</span>                      <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;C&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [347]: </span><span class="n">df_mi</span>
<span class="gr">Out[347]: </span>
<span class="go">                  A         B         C</span>
<span class="go">foo bar                                </span>
<span class="go">foo one   -0.584718  0.816594 -0.081947</span>
<span class="go">    two   -0.344766  0.528288 -1.068989</span>
<span class="go">    three -0.511881  0.291205  0.566534</span>
<span class="go">bar one    0.503592  0.285296  0.484288</span>
<span class="go">    two    1.363482 -0.781105 -0.468018</span>
<span class="go">baz two    1.224574 -1.281108  0.875476</span>
<span class="go">    three -1.710715 -0.450765  0.749164</span>
<span class="go">qux one   -0.203933 -0.182175  0.680656</span>
<span class="go">    two   -1.818499  0.047072  0.394844</span>
<span class="go">    three -0.248432 -0.617707 -0.682884</span>

<span class="gp">In [348]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;df_mi&apos;</span><span class="p">,</span><span class="n">df_mi</span><span class="p">)</span>

<span class="gp">In [349]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df_mi&apos;</span><span class="p">)</span>
<span class="gr">Out[349]: </span>
<span class="go">                  A         B         C</span>
<span class="go">foo bar                                </span>
<span class="go">foo one   -0.584718  0.816594 -0.081947</span>
<span class="go">    two   -0.344766  0.528288 -1.068989</span>
<span class="go">    three -0.511881  0.291205  0.566534</span>
<span class="go">bar one    0.503592  0.285296  0.484288</span>
<span class="go">    two    1.363482 -0.781105 -0.468018</span>
<span class="go">baz two    1.224574 -1.281108  0.875476</span>
<span class="go">    three -1.710715 -0.450765  0.749164</span>
<span class="go">qux one   -0.203933 -0.182175  0.680656</span>
<span class="go">    two   -1.818499  0.047072  0.394844</span>
<span class="go">    three -0.248432 -0.617707 -0.682884</span>

<span class="c"># the levels are automatically included as data columns</span>
<span class="gp">In [350]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df_mi&apos;</span><span class="p">,</span> <span class="s1">&apos;foo=bar&apos;</span><span class="p">)</span>
<span class="gr">Out[350]: </span>
<span class="go">                A         B         C</span>
<span class="go">foo bar                              </span>
<span class="go">bar one  0.503592  0.285296  0.484288</span>
<span class="go">    two  1.363482 -0.781105 -0.468018</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="querying">
<span id="io-hdf5-query"></span><h3><span class="yiyi-st" id="yiyi-1178">Querying</span></h3>
<div class="section" id="querying-a-table">
<h4><span class="yiyi-st" id="yiyi-1179">Querying a Table</span></h4>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1180">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1181">此查询功能在<code class="docutils literal"><span class="pre">0.13.0</span></code>中实质性更改。</span><span class="yiyi-st" id="yiyi-1182">如果其不是字符串类型，则接受来自先前版本的查询（使用<code class="docutils literal"><span class="pre">DeprecationWarning</span></code>）。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1183"><code class="docutils literal"><span class="pre">select</span></code>和<code class="docutils literal"><span class="pre">delete</span></code>操作具有可选择的标准，可以指定仅选择/删除数据的子集。</span><span class="yiyi-st" id="yiyi-1184">这允许有一个非常大的磁盘表，并且只检索一部分数据。</span></p>
<p><span class="yiyi-st" id="yiyi-1185">使用<code class="docutils literal"><span class="pre">Term</span></code>类指定查询作为布尔表达式。</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-1186"><code class="docutils literal"><span class="pre">index</span></code>和<code class="docutils literal"><span class="pre">columns</span></code>是DataFrame的支持索引器</span></li>
<li><span class="yiyi-st" id="yiyi-1187"><code class="docutils literal"><span class="pre">major_axis</span></code>，<code class="docutils literal"><span class="pre">minor_axis</span></code>和<code class="docutils literal"><span class="pre">items</span></code>是面板的支持索引器</span></li>
<li><span class="yiyi-st" id="yiyi-1188">如果指定<code class="docutils literal"><span class="pre">data_columns</span></code>，这些可以用作附加索引器</span></li>
</ul>
<p><span class="yiyi-st" id="yiyi-1189">有效的比较运算符为：</span></p>
<p><span class="yiyi-st" id="yiyi-1190"><code class="docutils literal"><span class="pre">=，</span> <span class="pre">==，</span> <span class="pre">！=，</span> <span class="pre">＆gt；，</span> <span class="pre">/ t5&gt; <span class="pre">＆lt；，</span> <span class="pre">＆lt； =</span></span></code></span></p>
<p><span class="yiyi-st" id="yiyi-1191">有效的布尔表达式结合：</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-1192"><code class="docutils literal"><span class="pre">|</span></code>：或</span></li>
<li><span class="yiyi-st" id="yiyi-1193"><code class="docutils literal"><span class="pre">&amp;</span></code>：和</span></li>
<li><span class="yiyi-st" id="yiyi-1194"><code class="docutils literal"><span class="pre">(</span></code>and<code class="docutils literal"><span class="pre">)</span></code>：用于分组</span></li>
</ul>
<p><span class="yiyi-st" id="yiyi-1195">这些规则类似于在pandas中用于索引的布尔表达式。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1196">注意</span></p>
<ul class="last simple">
<li><span class="yiyi-st" id="yiyi-1197"><code class="docutils literal"><span class="pre">=</span></code>将自动展开为比较运算符<code class="docutils literal"><span class="pre">==</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1198"><code class="docutils literal"><span class="pre">~</span></code>是非运算符，但只能在非常有限的情况下使用</span></li>
<li><span class="yiyi-st" id="yiyi-1199">如果传递表达式的列表/元组，则它们将通过<code class="docutils literal"><span class="pre">&amp;</span></code>组合</span></li>
</ul>
</div>
<p><span class="yiyi-st" id="yiyi-1200">以下是有效的表达式：</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-1201"><code class="docutils literal"><span class="pre">&apos;index&gt;=date&apos;</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1202"><code class="docutils literal"><span class="pre">“columns = [&apos;A&apos;，</span> <span class="pre">&apos;D&apos;]”</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1203"><code class="docutils literal"><span class="pre">&quot;columns</span> <span class="pre">in</span> <span class="pre">[&apos;A&apos;,</span> <span class="pre">&apos;D&apos;]&quot;</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1204"><code class="docutils literal"><span class="pre">&apos;columns=A&apos;</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1205"><code class="docutils literal"><span class="pre">&apos;columns==A&apos;</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1206"><code class="docutils literal"><span class="pre">&quot;~(columns=[&apos;A&apos;,&apos;B&apos;])&quot;</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1207"><code class="docutils literal"><span class="pre">&apos;index＆gt； df.index [3]</span> <span class="pre">＆amp；</span> <span class="pre">string =“bar”&apos;</span> </code></span></li>
<li><span class="yiyi-st" id="yiyi-1208"><code class="docutils literal"><span class="pre">&apos;（索引＆gt； df.index [3]</span> <span class="pre">＆amp；</span> <span class="pre">索引 <span class="pre"></span> <span class="pre">string =“bar”&apos;</span></span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1209"><code class="docutils literal"><span class="pre">&quot;ts&gt;=Timestamp(&apos;2012-02-01&apos;)&quot;</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1210"><code class="docutils literal"><span class="pre">&quot;major_axis&gt;=20130101&quot;</span></code></span></li>
</ul>
<p><span class="yiyi-st" id="yiyi-1211"><code class="docutils literal"><span class="pre">indexers</span></code>位于子表达式的左侧：</span></p>
<p><span class="yiyi-st" id="yiyi-1212"><code class="docutils literal"><span class="pre">columns</span></code>，<code class="docutils literal"><span class="pre">major_axis</span></code>，<code class="docutils literal"><span class="pre">ts</span></code></span></p>
<p><span class="yiyi-st" id="yiyi-1213">子表达式的右侧（在比较运算符之后）可以是：</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-1214">将被评估的函数，例如。 <code class="docutils literal"><span class="pre">Timestamp(&apos;2012-02-01&apos;)</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1215">字符串，例如<code class="docutils literal"><span class="pre">&quot;bar&quot;</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1216">日期样。 <code class="docutils literal"><span class="pre">20130101</span></code>或<code class="docutils literal"><span class="pre">&quot;20130101&quot;</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1217">列表，例如。 <code class="docutils literal"><span class="pre">&quot;[&apos;A&apos;,&apos;B&apos;]&quot;</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1218">在本地名称空间中定义的变量，例如。 <code class="docutils literal"><span class="pre">date</span></code></span></li>
</ul>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1219">注意</span></p>
<p><span class="yiyi-st" id="yiyi-1220">不推荐通过将字符串插入查询表达式来将字符串传递给查询。</span><span class="yiyi-st" id="yiyi-1221">只需将感兴趣的字符串分配给变量，并在表达式中使用该变量。</span><span class="yiyi-st" id="yiyi-1222">例如，这样做</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">string</span> <span class="o">=</span> <span class="s2">&quot;HolyMoly&apos;&quot;</span>
<span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">,</span> <span class="s1">&apos;index == string&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1223">而不是这个</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">string</span> <span class="o">=</span> <span class="s2">&quot;HolyMoly&apos;&quot;</span>
<span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">,</span>  <span class="s1">&apos;index == </span><span class="si">%s</span><span class="s1">&apos;</span> <span class="o">%</span> <span class="n">string</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1224">后者将<strong>不</strong>工作，并会引发<code class="docutils literal"><span class="pre">SyntaxError</span></code>。注意，在<code class="docutils literal"><span class="pre">string</span></code>变量中有一个单引号后跟一个双引号。</span></p>
<p><span class="yiyi-st" id="yiyi-1225">如果<em>必须</em>插值，请使用<code class="docutils literal"><span class="pre">&apos;%r&apos;</span></code>格式说明符</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">,</span> <span class="s1">&apos;index == </span><span class="si">%r</span><span class="s1">&apos;</span> <span class="o">%</span> <span class="n">string</span><span class="p">)</span>
</pre></div>
</div>
<p class="last"><span class="yiyi-st" id="yiyi-1226">其将引用<code class="docutils literal"><span class="pre">string</span></code>。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1227">这里有些例子：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [351]: </span><span class="n">dfq</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span><span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;ABCD&apos;</span><span class="p">),</span><span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">,</span><span class="n">periods</span><span class="o">=</span><span class="mi">10</span><span class="p">))</span>

<span class="gp">In [352]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;dfq&apos;</span><span class="p">,</span><span class="n">dfq</span><span class="p">,</span><span class="n">format</span><span class="o">=</span><span class="s1">&apos;table&apos;</span><span class="p">,</span><span class="n">data_columns</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1228">使用布尔表达式，具有行内函数评估。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [353]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;dfq&apos;</span><span class="p">,</span><span class="s2">&quot;index&gt;pd.Timestamp(&apos;20130104&apos;) &amp; columns=[&apos;A&apos;, &apos;B&apos;]&quot;</span><span class="p">)</span>
<span class="gr">Out[353]: </span>
<span class="go">                   A         B</span>
<span class="go">2013-01-05  1.210384  0.797435</span>
<span class="go">2013-01-06 -0.850346  1.176812</span>
<span class="go">2013-01-07  0.984188 -0.121728</span>
<span class="go">2013-01-08  0.796595 -0.474021</span>
<span class="go">2013-01-09 -0.804834 -2.123620</span>
<span class="go">2013-01-10  0.334198  0.536784</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1229">使用和内联列引用</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [354]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;dfq&apos;</span><span class="p">,</span><span class="n">where</span><span class="o">=</span><span class="s2">&quot;A&gt;0 or C&gt;0&quot;</span><span class="p">)</span>
<span class="gr">Out[354]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2013-01-01  0.436258 -1.703013  0.393711 -0.479324</span>
<span class="go">2013-01-02 -0.299016  0.694103  0.678630  0.239556</span>
<span class="go">2013-01-03  0.151227  0.816127  1.893534  0.639633</span>
<span class="go">2013-01-04 -0.962029 -2.085266  1.930247 -1.735349</span>
<span class="go">2013-01-05  1.210384  0.797435 -0.379811  0.702562</span>
<span class="go">2013-01-07  0.984188 -0.121728  2.365769  0.496143</span>
<span class="go">2013-01-08  0.796595 -0.474021 -0.056696  1.357797</span>
<span class="go">2013-01-10  0.334198  0.536784 -0.743830 -0.320204</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1230">也与小组一起工作。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [355]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;wp&apos;</span><span class="p">,</span><span class="n">wp</span><span class="p">)</span>

<span class="gp">In [356]: </span><span class="n">store</span>
<span class="gr">Out[356]: </span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: store.h5</span>
<span class="go">/df                     frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;3,indexers-&gt;[index])                     </span>
<span class="go">/df_mi                  frame_table  (typ-&gt;appendable_multi,nrows-&gt;10,ncols-&gt;5,indexers-&gt;[index],dc-&gt;[bar,foo])</span>
<span class="go">/df_mixed               frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;7,indexers-&gt;[index])                     </span>
<span class="go">/dfq                    frame_table  (typ-&gt;appendable,nrows-&gt;10,ncols-&gt;4,indexers-&gt;[index],dc-&gt;[A,B,C,D])      </span>
<span class="go">/foo/bar/bah            frame        (shape-&gt;[8,3])                                                            </span>
<span class="go">/wp                     wide_table   (typ-&gt;appendable,nrows-&gt;20,ncols-&gt;2,indexers-&gt;[major_axis,minor_axis])    </span>

<span class="gp">In [357]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;wp&apos;</span><span class="p">,</span> <span class="s2">&quot;major_axis&gt;pd.Timestamp(&apos;20000102&apos;) &amp; minor_axis=[&apos;A&apos;, &apos;B&apos;]&quot;</span><span class="p">)</span>
<span class="gr">Out[357]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 2 (items) x 3 (major_axis) x 2 (minor_axis)</span>
<span class="go">Items axis: Item1 to Item2</span>
<span class="go">Major_axis axis: 2000-01-03 00:00:00 to 2000-01-05 00:00:00</span>
<span class="go">Minor_axis axis: A to B</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1231">可以提供<code class="docutils literal"><span class="pre">columns</span></code>关键字来选择要返回的列的列表，这相当于传递<code class="docutils literal"><span class="pre">&apos;columns=list_of_columns_to_filter&apos;</span></code>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [358]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">,</span> <span class="s2">&quot;columns=[&apos;A&apos;, &apos;B&apos;]&quot;</span><span class="p">)</span>
<span class="gr">Out[358]: </span>
<span class="go">                   A         B</span>
<span class="go">2000-01-01  0.887163  0.859588</span>
<span class="go">2000-01-02  0.015696 -2.242685</span>
<span class="go">2000-01-03  0.991946  0.953324</span>
<span class="go">2000-01-04 -0.334077  0.002118</span>
<span class="go">2000-01-05  0.289092  1.321158</span>
<span class="go">2000-01-06 -0.202646 -0.655969</span>
<span class="go">2000-01-07  0.553439  1.318152</span>
<span class="go">2000-01-08  0.675554 -1.817027</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1232">可以指定<code class="docutils literal"><span class="pre">start</span></code>和<code class="docutils literal"><span class="pre">stop</span></code>参数来限制总搜索空间。</span><span class="yiyi-st" id="yiyi-1233">这些是根据表中的总行数。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># this is effectively what the storage of a Panel looks like</span>
<span class="gp">In [359]: </span><span class="n">wp</span><span class="o">.</span><span class="n">to_frame</span><span class="p">()</span>
<span class="gr">Out[359]: </span>
<span class="go">                     Item1     Item2</span>
<span class="go">major      minor                    </span>
<span class="go">2000-01-01 A      1.058969  0.215269</span>
<span class="go">           B     -0.397840  0.841009</span>
<span class="go">           C      0.337438 -1.445810</span>
<span class="go">           D      1.047579 -1.401973</span>
<span class="go">2000-01-02 A      1.045938 -0.100918</span>
<span class="go">           B      0.863717 -0.548242</span>
<span class="go">           C     -0.122092 -0.144620</span>
<span class="go">...                    ...       ...</span>
<span class="go">2000-01-04 B      0.036142  0.307969</span>
<span class="go">           C     -2.074978 -0.208499</span>
<span class="go">           D      0.247792  1.033801</span>
<span class="go">2000-01-05 A     -0.897157 -2.400454</span>
<span class="go">           B     -0.136795  2.030604</span>
<span class="go">           C      0.018289 -1.142631</span>
<span class="go">           D      0.755414  0.211883</span>

<span class="go">[20 rows x 2 columns]</span>

<span class="c"># limiting the search</span>
<span class="gp">In [360]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;wp&apos;</span><span class="p">,</span><span class="s2">&quot;major_axis&gt;20000102 &amp; minor_axis=[&apos;A&apos;,&apos;B&apos;]&quot;</span><span class="p">,</span>
<span class="gp">   .....:</span>              <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[360]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 2 (items) x 1 (major_axis) x 2 (minor_axis)</span>
<span class="go">Items axis: Item1 to Item2</span>
<span class="go">Major_axis axis: 2000-01-03 00:00:00 to 2000-01-03 00:00:00</span>
<span class="go">Minor_axis axis: A to B</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1234">注意</span></p>
<p><span class="yiyi-st" id="yiyi-1235">如果查询表达式具有未知的变量引用，则<code class="docutils literal"><span class="pre">select</span></code>将引发<code class="docutils literal"><span class="pre">ValueError</span></code>。</span><span class="yiyi-st" id="yiyi-1236">通常这意味着您试图在<strong>而不是</strong>数据列的列上选择。</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1237">如果查询表达式无效，则<code class="docutils literal"><span class="pre">select</span></code>会引发<code class="docutils literal"><span class="pre">SyntaxError</span></code>。</span></p>
</div>
</div>
<div class="section" id="using-timedelta64-ns">
<span id="io-hdf5-timedelta"></span><h4><span class="yiyi-st" id="yiyi-1238">Using timedelta64[ns]</span></h4>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1239"><span class="versionmodified">版本0.13中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1240">从0.13.0开始，您可以使用<code class="docutils literal"><span class="pre">timedelta64[ns]</span></code>类型存储和查询。</span><span class="yiyi-st" id="yiyi-1241">Terms can be specified in the format: <code class="docutils literal"><span class="pre">&lt;float&gt;(&lt;unit&gt;)</span></code>, where float may be signed (and fractional), and unit can be <code class="docutils literal"><span class="pre">D,s,ms,us,ns</span></code> for the timedelta. </span><span class="yiyi-st" id="yiyi-1242">这里有一个例子：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [361]: </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">timedelta</span>

<span class="gp">In [362]: </span><span class="n">dftd</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">A</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">),</span> <span class="n">B</span> <span class="o">=</span> <span class="p">[</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">)</span> <span class="o">+</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">i</span><span class="p">,</span><span class="n">seconds</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="p">]))</span>

<span class="gp">In [363]: </span><span class="n">dftd</span><span class="p">[</span><span class="s1">&apos;C&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">dftd</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span><span class="o">-</span><span class="n">dftd</span><span class="p">[</span><span class="s1">&apos;B&apos;</span><span class="p">]</span>

<span class="gp">In [364]: </span><span class="n">dftd</span>
<span class="gr">Out[364]: </span>
<span class="go">           A                   B                  C</span>
<span class="go">0 2013-01-01 2013-01-01 00:00:10  -1 days +23:59:50</span>
<span class="go">1 2013-01-01 2013-01-02 00:00:10  -2 days +23:59:50</span>
<span class="go">2 2013-01-01 2013-01-03 00:00:10  -3 days +23:59:50</span>
<span class="go">3 2013-01-01 2013-01-04 00:00:10  -4 days +23:59:50</span>
<span class="go">4 2013-01-01 2013-01-05 00:00:10  -5 days +23:59:50</span>
<span class="go">5 2013-01-01 2013-01-06 00:00:10  -6 days +23:59:50</span>
<span class="go">6 2013-01-01 2013-01-07 00:00:10  -7 days +23:59:50</span>
<span class="go">7 2013-01-01 2013-01-08 00:00:10  -8 days +23:59:50</span>
<span class="go">8 2013-01-01 2013-01-09 00:00:10  -9 days +23:59:50</span>
<span class="go">9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50</span>

<span class="gp">In [365]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;dftd&apos;</span><span class="p">,</span><span class="n">dftd</span><span class="p">,</span><span class="n">data_columns</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [366]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;dftd&apos;</span><span class="p">,</span><span class="s2">&quot;C&lt;&apos;-3.5D&apos;&quot;</span><span class="p">)</span>
<span class="gr">Out[366]: </span>
<span class="go">           A                   B                  C</span>
<span class="go">4 2013-01-01 2013-01-05 00:00:10  -5 days +23:59:50</span>
<span class="go">5 2013-01-01 2013-01-06 00:00:10  -6 days +23:59:50</span>
<span class="go">6 2013-01-01 2013-01-07 00:00:10  -7 days +23:59:50</span>
<span class="go">7 2013-01-01 2013-01-08 00:00:10  -8 days +23:59:50</span>
<span class="go">8 2013-01-01 2013-01-09 00:00:10  -9 days +23:59:50</span>
<span class="go">9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50</span>
</pre></div>
</div>
</div>
<div class="section" id="indexing">
<h4><span class="yiyi-st" id="yiyi-1243">Indexing</span></h4>
<p><span class="yiyi-st" id="yiyi-1244">在数据已经在表中之后（在<code class="docutils literal"><span class="pre">append/put</span></code>操作之后），可以使用<code class="docutils literal"><span class="pre">create_table_index</span></code>创建/修改表的索引。</span><span class="yiyi-st" id="yiyi-1245">鼓励创建表索引<strong>高度</strong>。</span><span class="yiyi-st" id="yiyi-1246">当您使用索引维度作为<code class="docutils literal"><span class="pre">where</span></code>的<code class="docutils literal"><span class="pre">select</span></code>时，这将加快查询速度。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1247">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1248">在索引表和您指定的任何数据列上自动创建索引（起始<code class="docutils literal"><span class="pre">0.10.1</span></code>）。</span><span class="yiyi-st" id="yiyi-1249">可以通过将<code class="docutils literal"><span class="pre">index=False</span></code>传递给<code class="docutils literal"><span class="pre">append</span></code>来关闭此行为。</span></p>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># we have automagically already created an index (in the first section)</span>
<span class="gp">In [367]: </span><span class="n">i</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">root</span><span class="o">.</span><span class="n">df</span><span class="o">.</span><span class="n">table</span><span class="o">.</span><span class="n">cols</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">index</span>

<span class="gp">In [368]: </span><span class="n">i</span><span class="o">.</span><span class="n">optlevel</span><span class="p">,</span> <span class="n">i</span><span class="o">.</span><span class="n">kind</span>
<span class="gr">Out[368]: </span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="s1">&apos;medium&apos;</span><span class="p">)</span>

<span class="c"># change an index by passing new parameters</span>
<span class="gp">In [369]: </span><span class="n">store</span><span class="o">.</span><span class="n">create_table_index</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">,</span> <span class="n">optlevel</span><span class="o">=</span><span class="mi">9</span><span class="p">,</span> <span class="n">kind</span><span class="o">=</span><span class="s1">&apos;full&apos;</span><span class="p">)</span>

<span class="gp">In [370]: </span><span class="n">i</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">root</span><span class="o">.</span><span class="n">df</span><span class="o">.</span><span class="n">table</span><span class="o">.</span><span class="n">cols</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">index</span>

<span class="gp">In [371]: </span><span class="n">i</span><span class="o">.</span><span class="n">optlevel</span><span class="p">,</span> <span class="n">i</span><span class="o">.</span><span class="n">kind</span>
<span class="gr">Out[371]: </span><span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="s1">&apos;full&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1250">通常当将大量数据附加到内存时，关闭每个附加项的索引创建是有用的，然后在结束时重新创建。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [372]: </span><span class="n">df_1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span><span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;AB&apos;</span><span class="p">))</span>

<span class="gp">In [373]: </span><span class="n">df_2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span><span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;AB&apos;</span><span class="p">))</span>

<span class="gp">In [374]: </span><span class="n">st</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">HDFStore</span><span class="p">(</span><span class="s1">&apos;appends.h5&apos;</span><span class="p">,</span><span class="n">mode</span><span class="o">=</span><span class="s1">&apos;w&apos;</span><span class="p">)</span>

<span class="gp">In [375]: </span><span class="n">st</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">,</span> <span class="n">df_1</span><span class="p">,</span> <span class="n">data_columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;B&apos;</span><span class="p">],</span> <span class="n">index</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

<span class="gp">In [376]: </span><span class="n">st</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">,</span> <span class="n">df_2</span><span class="p">,</span> <span class="n">data_columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;B&apos;</span><span class="p">],</span> <span class="n">index</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

<span class="gp">In [377]: </span><span class="n">st</span><span class="o">.</span><span class="n">get_storer</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">table</span>
<span class="gr">Out[377]: </span>
<span class="go">/df/table (Table(20,)) &apos;&apos;</span>
<span class="go">  description := {</span>
<span class="go">  &quot;index&quot;: Int64Col(shape=(), dflt=0, pos=0),</span>
<span class="go">  &quot;values_block_0&quot;: Float64Col(shape=(1,), dflt=0.0, pos=1),</span>
<span class="go">  &quot;B&quot;: Float64Col(shape=(), dflt=0.0, pos=2)}</span>
<span class="go">  byteorder := &apos;little&apos;</span>
<span class="go">  chunkshape := (2730,)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1251">然后在完成附加时创建索引。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [378]: </span><span class="n">st</span><span class="o">.</span><span class="n">create_table_index</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;B&apos;</span><span class="p">],</span> <span class="n">optlevel</span><span class="o">=</span><span class="mi">9</span><span class="p">,</span> <span class="n">kind</span><span class="o">=</span><span class="s1">&apos;full&apos;</span><span class="p">)</span>

<span class="gp">In [379]: </span><span class="n">st</span><span class="o">.</span><span class="n">get_storer</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">table</span>
<span class="gr">Out[379]: </span>
<span class="go">/df/table (Table(20,)) &apos;&apos;</span>
<span class="go">  description := {</span>
<span class="go">  &quot;index&quot;: Int64Col(shape=(), dflt=0, pos=0),</span>
<span class="go">  &quot;values_block_0&quot;: Float64Col(shape=(1,), dflt=0.0, pos=1),</span>
<span class="go">  &quot;B&quot;: Float64Col(shape=(), dflt=0.0, pos=2)}</span>
<span class="go">  byteorder := &apos;little&apos;</span>
<span class="go">  chunkshape := (2730,)</span>
<span class="go">  autoindex := True</span>
<span class="go">  colindexes := {</span>
<span class="go">    &quot;B&quot;: Index(9, full, shuffle, zlib(1)).is_csi=True}</span>

<span class="gp">In [380]: </span><span class="n">st</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1252">有关如何在现有存储上创建完全排序索引（CSI）的信息，请参见<a class="reference external" href="http://stackoverflow.com/questions/17893370/ptrepack-sortby-needs-full-index">此处</a>。</span></p>
</div>
<div class="section" id="query-via-data-columns">
<h4><span class="yiyi-st" id="yiyi-1253">Query via Data Columns</span></h4>
<p><span class="yiyi-st" id="yiyi-1254">您可以指定（和建立索引）您希望能够执行查询的某些列（除了可以始终查询的<cite>可索引的</cite>列）。</span><span class="yiyi-st" id="yiyi-1255">例如，您想要执行此常见操作，磁盘上，并只返回匹配此查询的框架。</span><span class="yiyi-st" id="yiyi-1256">您可以指定<code class="docutils literal"><span class="pre">data_columns</span> <span class="pre">=</span> <span class="pre">True</span></code>以强制所有列为data_columns</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [381]: </span><span class="n">df_dc</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

<span class="gp">In [382]: </span><span class="n">df_dc</span><span class="p">[</span><span class="s1">&apos;string&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&apos;foo&apos;</span>

<span class="gp">In [383]: </span><span class="n">df_dc</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">4</span><span class="p">:</span><span class="mi">6</span><span class="p">,</span><span class="s1">&apos;string&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>

<span class="gp">In [384]: </span><span class="n">df_dc</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">7</span><span class="p">:</span><span class="mi">9</span><span class="p">,</span><span class="s1">&apos;string&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&apos;bar&apos;</span>

<span class="gp">In [385]: </span><span class="n">df_dc</span><span class="p">[</span><span class="s1">&apos;string2&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&apos;cool&apos;</span>

<span class="gp">In [386]: </span><span class="n">df_dc</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">,[</span><span class="s1">&apos;B&apos;</span><span class="p">,</span><span class="s1">&apos;C&apos;</span><span class="p">]]</span> <span class="o">=</span> <span class="mf">1.0</span>

<span class="gp">In [387]: </span><span class="n">df_dc</span>
<span class="gr">Out[387]: </span>
<span class="go">                   A         B         C string string2</span>
<span class="go">2000-01-01  0.887163  0.859588 -0.636524    foo    cool</span>
<span class="go">2000-01-02  0.015696  1.000000  1.000000    foo    cool</span>
<span class="go">2000-01-03  0.991946  1.000000  1.000000    foo    cool</span>
<span class="go">2000-01-04 -0.334077  0.002118  0.405453    foo    cool</span>
<span class="go">2000-01-05  0.289092  1.321158 -1.546906    NaN    cool</span>
<span class="go">2000-01-06 -0.202646 -0.655969  0.193421    NaN    cool</span>
<span class="go">2000-01-07  0.553439  1.318152 -0.469305    foo    cool</span>
<span class="go">2000-01-08  0.675554 -1.817027 -0.183109    bar    cool</span>

<span class="c"># on-disk operations</span>
<span class="gp">In [388]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;df_dc&apos;</span><span class="p">,</span> <span class="n">df_dc</span><span class="p">,</span> <span class="n">data_columns</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;C&apos;</span><span class="p">,</span> <span class="s1">&apos;string&apos;</span><span class="p">,</span> <span class="s1">&apos;string2&apos;</span><span class="p">])</span>

<span class="gp">In [389]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df_dc&apos;</span><span class="p">,</span> <span class="p">[</span> <span class="n">pd</span><span class="o">.</span><span class="n">Term</span><span class="p">(</span><span class="s1">&apos;B&gt;0&apos;</span><span class="p">)</span> <span class="p">])</span>
<span class="gr">Out[389]: </span>
<span class="go">                   A         B         C string string2</span>
<span class="go">2000-01-01  0.887163  0.859588 -0.636524    foo    cool</span>
<span class="go">2000-01-02  0.015696  1.000000  1.000000    foo    cool</span>
<span class="go">2000-01-03  0.991946  1.000000  1.000000    foo    cool</span>
<span class="go">2000-01-04 -0.334077  0.002118  0.405453    foo    cool</span>
<span class="go">2000-01-05  0.289092  1.321158 -1.546906    NaN    cool</span>
<span class="go">2000-01-07  0.553439  1.318152 -0.469305    foo    cool</span>

<span class="c"># getting creative</span>
<span class="gp">In [390]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df_dc&apos;</span><span class="p">,</span> <span class="s1">&apos;B &gt; 0 &amp; C &gt; 0 &amp; string == foo&apos;</span><span class="p">)</span>
<span class="gr">Out[390]: </span>
<span class="go">                   A         B         C string string2</span>
<span class="go">2000-01-02  0.015696  1.000000  1.000000    foo    cool</span>
<span class="go">2000-01-03  0.991946  1.000000  1.000000    foo    cool</span>
<span class="go">2000-01-04 -0.334077  0.002118  0.405453    foo    cool</span>

<span class="c"># this is in-memory version of this type of selection</span>
<span class="gp">In [391]: </span><span class="n">df_dc</span><span class="p">[(</span><span class="n">df_dc</span><span class="o">.</span><span class="n">B</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">df_dc</span><span class="o">.</span><span class="n">C</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">df_dc</span><span class="o">.</span><span class="n">string</span> <span class="o">==</span> <span class="s1">&apos;foo&apos;</span><span class="p">)]</span>
<span class="gr">Out[391]: </span>
<span class="go">                   A         B         C string string2</span>
<span class="go">2000-01-02  0.015696  1.000000  1.000000    foo    cool</span>
<span class="go">2000-01-03  0.991946  1.000000  1.000000    foo    cool</span>
<span class="go">2000-01-04 -0.334077  0.002118  0.405453    foo    cool</span>

<span class="c"># we have automagically created this index and the B/C/string/string2</span>
<span class="c"># columns are stored separately as ``PyTables`` columns</span>
<span class="gp">In [392]: </span><span class="n">store</span><span class="o">.</span><span class="n">root</span><span class="o">.</span><span class="n">df_dc</span><span class="o">.</span><span class="n">table</span>
<span class="gr">Out[392]: </span>
<span class="go">/df_dc/table (Table(8,)) &apos;&apos;</span>
<span class="go">  description := {</span>
<span class="go">  &quot;index&quot;: Int64Col(shape=(), dflt=0, pos=0),</span>
<span class="go">  &quot;values_block_0&quot;: Float64Col(shape=(1,), dflt=0.0, pos=1),</span>
<span class="go">  &quot;B&quot;: Float64Col(shape=(), dflt=0.0, pos=2),</span>
<span class="go">  &quot;C&quot;: Float64Col(shape=(), dflt=0.0, pos=3),</span>
<span class="go">  &quot;string&quot;: StringCol(itemsize=3, shape=(), dflt=&apos;&apos;, pos=4),</span>
<span class="go">  &quot;string2&quot;: StringCol(itemsize=4, shape=(), dflt=&apos;&apos;, pos=5)}</span>
<span class="go">  byteorder := &apos;little&apos;</span>
<span class="go">  chunkshape := (1680,)</span>
<span class="go">  autoindex := True</span>
<span class="go">  colindexes := {</span>
<span class="go">    &quot;index&quot;: Index(6, medium, shuffle, zlib(1)).is_csi=False,</span>
<span class="go">    &quot;C&quot;: Index(6, medium, shuffle, zlib(1)).is_csi=False,</span>
<span class="go">    &quot;B&quot;: Index(6, medium, shuffle, zlib(1)).is_csi=False,</span>
<span class="go">    &quot;string2&quot;: Index(6, medium, shuffle, zlib(1)).is_csi=False,</span>
<span class="go">    &quot;string&quot;: Index(6, medium, shuffle, zlib(1)).is_csi=False}</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1257">通过将大量列置入<cite>数据列</cite>中，会有一些性能下降，因此由用户指定这些列。</span><span class="yiyi-st" id="yiyi-1258">此外，你不能在第一次append / put操作后更改数据列（也不能索引）（当然你可以简单地读入数据并创建一个新表！）</span></p>
</div>
<div class="section" id="iterator">
<h4><span class="yiyi-st" id="yiyi-1259">Iterator</span></h4>
<p><span class="yiyi-st" id="yiyi-1260">从<code class="docutils literal"><span class="pre">0.11.0</span></code>开始，您可以传递<code class="docutils literal"><span class="pre">iterator=True</span></code>或<code class="docutils literal"><span class="pre">chunksize=number_in_a_chunk</span></code>到<code class="docutils literal"><span class="pre">select</span></code>和<code class="docutils literal"><span class="pre">select_as_multiple</span></code>在结果上返回一个迭代器。</span><span class="yiyi-st" id="yiyi-1261">默认值是在一个块中返回的50,000行。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [393]: </span><span class="k">for</span> <span class="n">df</span> <span class="ow">in</span> <span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">,</span> <span class="n">chunksize</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
<span class="gp">   .....:</span>    <span class="k">print</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
<span class="gp">   .....:</span> 
<span class="go">                   A         B         C</span>
<span class="go">2000-01-01  0.887163  0.859588 -0.636524</span>
<span class="go">2000-01-02  0.015696 -2.242685  1.150036</span>
<span class="go">2000-01-03  0.991946  0.953324 -2.021255</span>
<span class="go">                   A         B         C</span>
<span class="go">2000-01-04 -0.334077  0.002118  0.405453</span>
<span class="go">2000-01-05  0.289092  1.321158 -1.546906</span>
<span class="go">2000-01-06 -0.202646 -0.655969  0.193421</span>
<span class="go">                   A         B         C</span>
<span class="go">2000-01-07  0.553439  1.318152 -0.469305</span>
<span class="go">2000-01-08  0.675554 -1.817027 -0.183109</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1262">注意</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1263"><span class="versionmodified">版本0.12.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1264">您还可以使用具有<code class="docutils literal"><span class="pre">read_hdf</span></code>的迭代器，它将打开，然后在完成迭代后自动关闭存储。</span></p>
<div class="last highlight-python"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">df</span> <span class="ow">in</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="s1">&apos;store.h5&apos;</span><span class="p">,</span><span class="s1">&apos;df&apos;</span><span class="p">,</span> <span class="n">chunksize</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
    <span class="k">print</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-1265">请注意，chunksize关键字适用于<strong>源</strong>行。</span><span class="yiyi-st" id="yiyi-1266">因此，如果您正在执行查询，那么chunksize将细分表中的所有行并应用查询，在可能不等大小的块上返回一个迭代器。</span></p>
<p><span class="yiyi-st" id="yiyi-1267">下面是一个生成查询并使用它来创建大小相等的返回块的方法。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [394]: </span><span class="n">dfeq</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;number&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">11</span><span class="p">)})</span>

<span class="gp">In [395]: </span><span class="n">dfeq</span>
<span class="gr">Out[395]: </span>
<span class="go">   number</span>
<span class="go">0       1</span>
<span class="go">1       2</span>
<span class="go">2       3</span>
<span class="go">3       4</span>
<span class="go">4       5</span>
<span class="go">5       6</span>
<span class="go">6       7</span>
<span class="go">7       8</span>
<span class="go">8       9</span>
<span class="go">9      10</span>

<span class="gp">In [396]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;dfeq&apos;</span><span class="p">,</span> <span class="n">dfeq</span><span class="p">,</span> <span class="n">data_columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;number&apos;</span><span class="p">])</span>

<span class="gp">In [397]: </span><span class="k">def</span> <span class="nf">chunks</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
<span class="gp">   .....:</span>      <span class="k">return</span> <span class="p">[</span><span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">i</span><span class="o">+</span><span class="n">n</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">l</span><span class="p">),</span> <span class="n">n</span><span class="p">)]</span>
<span class="gp">   .....:</span> 

<span class="gp">In [398]: </span><span class="n">evens</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">10</span><span class="p">]</span>

<span class="gp">In [399]: </span><span class="n">coordinates</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">select_as_coordinates</span><span class="p">(</span><span class="s1">&apos;dfeq&apos;</span><span class="p">,</span><span class="s1">&apos;number=evens&apos;</span><span class="p">)</span>

<span class="gp">In [400]: </span><span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">chunks</span><span class="p">(</span><span class="n">coordinates</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span>
<span class="gp">   .....:</span>      <span class="k">print</span> <span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;dfeq&apos;</span><span class="p">,</span><span class="n">where</span><span class="o">=</span><span class="n">c</span><span class="p">)</span>
<span class="gp">   .....:</span> 
<span class="go">   number</span>
<span class="go">1       2</span>
<span class="go">3       4</span>
<span class="go">   number</span>
<span class="go">5       6</span>
<span class="go">7       8</span>
<span class="go">   number</span>
<span class="go">9      10</span>
</pre></div>
</div>
</div>
<div class="section" id="advanced-queries">
<h4><span class="yiyi-st" id="yiyi-1268">Advanced Queries</span></h4>
<div class="section" id="select-a-single-column">
<h5><span class="yiyi-st" id="yiyi-1269">Select a Single Column</span></h5>
<p><span class="yiyi-st" id="yiyi-1270">要检索单个可索引或数据列，请使用方法<code class="docutils literal"><span class="pre">select_column</span></code>。</span><span class="yiyi-st" id="yiyi-1271">例如，这将使您能够非常快地获取索引。</span><span class="yiyi-st" id="yiyi-1272">这些返回结果的<code class="docutils literal"><span class="pre">Series</span></code>，由行号索引。</span><span class="yiyi-st" id="yiyi-1273">它们目前不接受<code class="docutils literal"><span class="pre">where</span></code>选择器。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [401]: </span><span class="n">store</span><span class="o">.</span><span class="n">select_column</span><span class="p">(</span><span class="s1">&apos;df_dc&apos;</span><span class="p">,</span> <span class="s1">&apos;index&apos;</span><span class="p">)</span>
<span class="gr">Out[401]: </span>
<span class="go">0   2000-01-01</span>
<span class="go">1   2000-01-02</span>
<span class="go">2   2000-01-03</span>
<span class="go">3   2000-01-04</span>
<span class="go">4   2000-01-05</span>
<span class="go">5   2000-01-06</span>
<span class="go">6   2000-01-07</span>
<span class="go">7   2000-01-08</span>
<span class="go">Name: index, dtype: datetime64[ns]</span>

<span class="gp">In [402]: </span><span class="n">store</span><span class="o">.</span><span class="n">select_column</span><span class="p">(</span><span class="s1">&apos;df_dc&apos;</span><span class="p">,</span> <span class="s1">&apos;string&apos;</span><span class="p">)</span>
<span class="gr">Out[402]: </span>
<span class="go">0    foo</span>
<span class="go">1    foo</span>
<span class="go">2    foo</span>
<span class="go">3    foo</span>
<span class="go">4    NaN</span>
<span class="go">5    NaN</span>
<span class="go">6    foo</span>
<span class="go">7    bar</span>
<span class="go">Name: string, dtype: object</span>
</pre></div>
</div>
</div>
<div class="section" id="selecting-coordinates">
<span id="io-hdf5-selecting-coordinates"></span><h5><span class="yiyi-st" id="yiyi-1274">Selecting coordinates</span></h5>
<p><span class="yiyi-st" id="yiyi-1275">有时您想要获取查询的坐标（a.k.a索引位置）。</span><span class="yiyi-st" id="yiyi-1276">这将返回结果位置的<code class="docutils literal"><span class="pre">Int64Index</span></code>。</span><span class="yiyi-st" id="yiyi-1277">这些坐标也可以传递到后续的<code class="docutils literal"><span class="pre">where</span></code>操作。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [403]: </span><span class="n">df_coord</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span><span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20000101&apos;</span><span class="p">,</span><span class="n">periods</span><span class="o">=</span><span class="mi">1000</span><span class="p">))</span>

<span class="gp">In [404]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;df_coord&apos;</span><span class="p">,</span><span class="n">df_coord</span><span class="p">)</span>

<span class="gp">In [405]: </span><span class="n">c</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">select_as_coordinates</span><span class="p">(</span><span class="s1">&apos;df_coord&apos;</span><span class="p">,</span><span class="s1">&apos;index&gt;20020101&apos;</span><span class="p">)</span>

<span class="gp">In [406]: </span><span class="n">c</span><span class="o">.</span><span class="n">summary</span><span class="p">()</span>
<span class="gr">Out[406]: </span><span class="s1">u&apos;Int64Index: 268 entries, 732 to 999&apos;</span>

<span class="gp">In [407]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df_coord&apos;</span><span class="p">,</span><span class="n">where</span><span class="o">=</span><span class="n">c</span><span class="p">)</span>
<span class="gr">Out[407]: </span>
<span class="go">                   0         1</span>
<span class="go">2002-01-02 -0.178266 -0.064638</span>
<span class="go">2002-01-03 -1.204956 -3.880898</span>
<span class="go">2002-01-04  0.974470  0.415160</span>
<span class="go">2002-01-05  1.751967  0.485011</span>
<span class="go">2002-01-06 -0.170894  0.748870</span>
<span class="go">2002-01-07  0.629793  0.811053</span>
<span class="go">2002-01-08  2.133776  0.238459</span>
<span class="go">...              ...       ...</span>
<span class="go">2002-09-20 -0.181434  0.612399</span>
<span class="go">2002-09-21 -0.763324 -0.354962</span>
<span class="go">2002-09-22 -0.261776  0.812126</span>
<span class="go">2002-09-23  0.482615 -0.886512</span>
<span class="go">2002-09-24 -0.037757 -0.562953</span>
<span class="go">2002-09-25  0.897706  0.383232</span>
<span class="go">2002-09-26 -1.324806  1.139269</span>

<span class="go">[268 rows x 2 columns]</span>
</pre></div>
</div>
</div>
<div class="section" id="selecting-using-a-where-mask">
<span id="io-hdf5-where-mask"></span><h5><span class="yiyi-st" id="yiyi-1278">Selecting using a where mask</span></h5>
<p><span class="yiyi-st" id="yiyi-1279">有时，您的查询可能涉及创建要选择的行的列表。</span><span class="yiyi-st" id="yiyi-1280">通常，此<code class="docutils literal"><span class="pre">mask</span></code>将是来自索引操作的结果<code class="docutils literal"><span class="pre">index</span></code>。</span><span class="yiyi-st" id="yiyi-1281">此示例选择datetimeindex的月份为5。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [408]: </span><span class="n">df_mask</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span><span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20000101&apos;</span><span class="p">,</span><span class="n">periods</span><span class="o">=</span><span class="mi">1000</span><span class="p">))</span>

<span class="gp">In [409]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;df_mask&apos;</span><span class="p">,</span><span class="n">df_mask</span><span class="p">)</span>

<span class="gp">In [410]: </span><span class="n">c</span> <span class="o">=</span> <span class="n">store</span><span class="o">.</span><span class="n">select_column</span><span class="p">(</span><span class="s1">&apos;df_mask&apos;</span><span class="p">,</span><span class="s1">&apos;index&apos;</span><span class="p">)</span>

<span class="gp">In [411]: </span><span class="n">where</span> <span class="o">=</span> <span class="n">c</span><span class="p">[</span><span class="n">pd</span><span class="o">.</span><span class="n">DatetimeIndex</span><span class="p">(</span><span class="n">c</span><span class="p">)</span><span class="o">.</span><span class="n">month</span><span class="o">==</span><span class="mi">5</span><span class="p">]</span><span class="o">.</span><span class="n">index</span>

<span class="gp">In [412]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df_mask&apos;</span><span class="p">,</span><span class="n">where</span><span class="o">=</span><span class="n">where</span><span class="p">)</span>
<span class="gr">Out[412]: </span>
<span class="go">                   0         1</span>
<span class="go">2000-05-01 -1.006245 -0.616759</span>
<span class="go">2000-05-02  0.218940  0.717838</span>
<span class="go">2000-05-03  0.013333  1.348060</span>
<span class="go">2000-05-04  0.662176 -1.050645</span>
<span class="go">2000-05-05 -1.034870 -0.243242</span>
<span class="go">2000-05-06 -0.753366 -1.454329</span>
<span class="go">2000-05-07 -1.022920 -0.476989</span>
<span class="go">...              ...       ...</span>
<span class="go">2002-05-25 -0.509090 -0.389376</span>
<span class="go">2002-05-26  0.150674  1.164337</span>
<span class="go">2002-05-27 -0.332944  0.115181</span>
<span class="go">2002-05-28 -1.048127 -0.605733</span>
<span class="go">2002-05-29  1.418754 -0.442835</span>
<span class="go">2002-05-30 -0.433200  0.835001</span>
<span class="go">2002-05-31 -1.041278  1.401811</span>

<span class="go">[93 rows x 2 columns]</span>
</pre></div>
</div>
</div>
<div class="section" id="storer-object">
<h5><span class="yiyi-st" id="yiyi-1282">Storer Object</span></h5>
<p><span class="yiyi-st" id="yiyi-1283">如果要检查存储的对象，请通过<code class="docutils literal"><span class="pre">get_storer</span></code>检索。</span><span class="yiyi-st" id="yiyi-1284">你可以使用这个程序来说明获取对象中的行数。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [413]: </span><span class="n">store</span><span class="o">.</span><span class="n">get_storer</span><span class="p">(</span><span class="s1">&apos;df_dc&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">nrows</span>
<span class="gr">Out[413]: </span><span class="mi">8</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="multiple-table-queries">
<h4><span class="yiyi-st" id="yiyi-1285">Multiple Table Queries</span></h4>
<p><span class="yiyi-st" id="yiyi-1286">0.10.1中的新增功能是方法<code class="docutils literal"><span class="pre">append_to_multiple</span></code>和<code class="docutils literal"><span class="pre">select_as_multiple</span></code>，可以一次执行从多个表中追加/选择。</span><span class="yiyi-st" id="yiyi-1287">想法是有一个表（称为选择器表），您索引最多/所有的列，并执行您的查询。</span><span class="yiyi-st" id="yiyi-1288">其他表是具有与选择器表的索引匹配的索引的数据表。</span><span class="yiyi-st" id="yiyi-1289">然后，您可以对选择器表执行非常快速的查询，但返回大量数据。</span><span class="yiyi-st" id="yiyi-1290">此方法类似于具有非常宽的表，但是可以实现更高效的查询。</span></p>
<p><span class="yiyi-st" id="yiyi-1291"><code class="docutils literal"><span class="pre">append_to_multiple</span></code>方法根据<code class="docutils literal"><span class="pre">d</span></code>将给定的单个DataFrame拆分为多个表，该字典将表名映射到该表中所需的“列”列表。</span><span class="yiyi-st" id="yiyi-1292">如果使用<cite>None</cite>代替列表，则该表将具有给定DataFrame的其余未指定列。</span><span class="yiyi-st" id="yiyi-1293">参数<code class="docutils literal"><span class="pre">selector</span></code>定义哪个表是选择器表（您可以从中进行查询）。</span><span class="yiyi-st" id="yiyi-1294">参数<code class="docutils literal"><span class="pre">dropna</span></code>将从输入DataFrame中删除行以确保表同步。</span><span class="yiyi-st" id="yiyi-1295">这意味着，如果写入的其中一个表的行完全为<code class="docutils literal"><span class="pre">np.NaN</span></code>，那么该行将从所有表中删除。</span></p>
<p><span class="yiyi-st" id="yiyi-1296">如果<code class="docutils literal"><span class="pre">dropna</span></code>为False，则<strong>用户负责同步表格</strong>。</span><span class="yiyi-st" id="yiyi-1297">Remember that entirely <code class="docutils literal"><span class="pre">np.Nan</span></code> rows are not written to the HDFStore, so if you choose to call <code class="docutils literal"><span class="pre">dropna=False</span></code>, some tables may have more rows than others, and therefore <code class="docutils literal"><span class="pre">select_as_multiple</span></code> may not work or it may return unexpected results.</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [414]: </span><span class="n">df_mt</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="mi">6</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;1/1/2000&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">8</span><span class="p">),</span>
<span class="gp">   .....:</span>                                   <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;C&apos;</span><span class="p">,</span> <span class="s1">&apos;D&apos;</span><span class="p">,</span> <span class="s1">&apos;E&apos;</span><span class="p">,</span> <span class="s1">&apos;F&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [415]: </span><span class="n">df_mt</span><span class="p">[</span><span class="s1">&apos;foo&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&apos;bar&apos;</span>

<span class="gp">In [416]: </span><span class="n">df_mt</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">)]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>

<span class="c"># you can also create the tables individually</span>
<span class="gp">In [417]: </span><span class="n">store</span><span class="o">.</span><span class="n">append_to_multiple</span><span class="p">({</span><span class="s1">&apos;df1_mt&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">],</span> <span class="s1">&apos;df2_mt&apos;</span><span class="p">:</span> <span class="bp">None</span> <span class="p">},</span>
<span class="gp">   .....:</span>                           <span class="n">df_mt</span><span class="p">,</span> <span class="n">selector</span><span class="o">=</span><span class="s1">&apos;df1_mt&apos;</span><span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [418]: </span><span class="n">store</span>
<span class="gr">Out[418]: </span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: store.h5</span>
<span class="go">/df                     frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;3,indexers-&gt;[index])                         </span>
<span class="go">/df1_mt                 frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;2,indexers-&gt;[index],dc-&gt;[A,B])               </span>
<span class="go">/df2_mt                 frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;5,indexers-&gt;[index])                         </span>
<span class="go">/df_coord               frame_table  (typ-&gt;appendable,nrows-&gt;1000,ncols-&gt;2,indexers-&gt;[index])                      </span>
<span class="go">/df_dc                  frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;5,indexers-&gt;[index],dc-&gt;[B,C,string,string2])</span>
<span class="go">/df_mask                frame_table  (typ-&gt;appendable,nrows-&gt;1000,ncols-&gt;2,indexers-&gt;[index])                      </span>
<span class="go">/df_mi                  frame_table  (typ-&gt;appendable_multi,nrows-&gt;10,ncols-&gt;5,indexers-&gt;[index],dc-&gt;[bar,foo])    </span>
<span class="go">/df_mixed               frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;7,indexers-&gt;[index])                         </span>
<span class="go">/dfeq                   frame_table  (typ-&gt;appendable,nrows-&gt;10,ncols-&gt;1,indexers-&gt;[index],dc-&gt;[number])           </span>
<span class="go">/dfq                    frame_table  (typ-&gt;appendable,nrows-&gt;10,ncols-&gt;4,indexers-&gt;[index],dc-&gt;[A,B,C,D])          </span>
<span class="go">/dftd                   frame_table  (typ-&gt;appendable,nrows-&gt;10,ncols-&gt;3,indexers-&gt;[index],dc-&gt;[A,B,C])            </span>
<span class="go">/foo/bar/bah            frame        (shape-&gt;[8,3])                                                                </span>
<span class="go">/wp                     wide_table   (typ-&gt;appendable,nrows-&gt;20,ncols-&gt;2,indexers-&gt;[major_axis,minor_axis])        </span>

<span class="c"># individual tables were created</span>
<span class="gp">In [419]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df1_mt&apos;</span><span class="p">)</span>
<span class="gr">Out[419]: </span>
<span class="go">                   A         B</span>
<span class="go">2000-01-01  0.714697  0.318215</span>
<span class="go">2000-01-02       NaN       NaN</span>
<span class="go">2000-01-03 -0.086919  0.416905</span>
<span class="go">2000-01-04  0.489131 -0.253340</span>
<span class="go">2000-01-05 -0.382952 -0.397373</span>
<span class="go">2000-01-06  0.538116  0.226388</span>
<span class="go">2000-01-07 -2.073479 -0.115926</span>
<span class="go">2000-01-08 -0.695400  0.402493</span>

<span class="gp">In [420]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;df2_mt&apos;</span><span class="p">)</span>
<span class="gr">Out[420]: </span>
<span class="go">                   C         D         E         F  foo</span>
<span class="go">2000-01-01  0.607460  0.790907  0.852225  0.096696  bar</span>
<span class="go">2000-01-02  0.811031 -0.356817  1.047085  0.664705  bar</span>
<span class="go">2000-01-03 -0.764381 -0.287229 -0.089351 -1.035115  bar</span>
<span class="go">2000-01-04 -1.948100 -0.116556  0.800597 -0.796154  bar</span>
<span class="go">2000-01-05 -0.717627  0.156995 -0.344718 -0.171208  bar</span>
<span class="go">2000-01-06  1.541729  0.205256  1.998065  0.953591  bar</span>
<span class="go">2000-01-07  1.391070  0.303013  1.093347 -0.101000  bar</span>
<span class="go">2000-01-08 -1.507639  0.089575  0.658822 -1.037627  bar</span>

<span class="c"># as a multiple</span>
<span class="gp">In [421]: </span><span class="n">store</span><span class="o">.</span><span class="n">select_as_multiple</span><span class="p">([</span><span class="s1">&apos;df1_mt&apos;</span><span class="p">,</span> <span class="s1">&apos;df2_mt&apos;</span><span class="p">],</span> <span class="n">where</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&gt;0&apos;</span><span class="p">,</span> <span class="s1">&apos;B&gt;0&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                           <span class="n">selector</span> <span class="o">=</span> <span class="s1">&apos;df1_mt&apos;</span><span class="p">)</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[421]: </span>
<span class="go">                   A         B         C         D         E         F  foo</span>
<span class="go">2000-01-01  0.714697  0.318215  0.607460  0.790907  0.852225  0.096696  bar</span>
<span class="go">2000-01-06  0.538116  0.226388  1.541729  0.205256  1.998065  0.953591  bar</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="delete-from-a-table">
<h3><span class="yiyi-st" id="yiyi-1298">Delete from a Table</span></h3>
<p><span class="yiyi-st" id="yiyi-1299">您可以通过指定<code class="docutils literal"><span class="pre">where</span></code>选择性地从表中删除。</span><span class="yiyi-st" id="yiyi-1300">在删除行时，了解<code class="docutils literal"><span class="pre">PyTables</span></code>通过擦除行，然后<strong>移动</strong>以下数据删除行很重要。</span><span class="yiyi-st" id="yiyi-1301">因此，删除可能是一个非常昂贵的操作，具体取决于数据的方向。</span><span class="yiyi-st" id="yiyi-1302">这在更高维度的物体（<code class="docutils literal"><span class="pre">Panel</span></code>和<code class="docutils literal"><span class="pre">Panel4D</span></code>）中尤其如此。</span><span class="yiyi-st" id="yiyi-1303">为了获得最佳效果，您需要将要删除的维度作为<code class="docutils literal"><span class="pre">indexables</span></code>中的第一个维度。</span></p>
<p><span class="yiyi-st" id="yiyi-1304">数据按照<code class="docutils literal"><span class="pre">indexables</span></code>的顺序排列（在磁盘上）。</span><span class="yiyi-st" id="yiyi-1305">这里有一个简单的用例。</span><span class="yiyi-st" id="yiyi-1306">您存储面板类型数据，日期在<code class="docutils literal"><span class="pre">major_axis</span></code>中，而ID在<code class="docutils literal"><span class="pre">minor_axis</span></code>中。</span><span class="yiyi-st" id="yiyi-1307">然后数据交织如下：</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-1308">date_1  -  id_1  -  id_2  - 。</span><span class="yiyi-st" id="yiyi-1309"> -  id_n</span></li>
<li><span class="yiyi-st" id="yiyi-1310">date_2  -  id_1  - 。</span><span class="yiyi-st" id="yiyi-1311"> -  id_n</span></li>
</ul>
<p><span class="yiyi-st" id="yiyi-1312">应当清楚，对<code class="docutils literal"><span class="pre">major_axis</span></code>的删除操作将会相当快速，因为删除了一个块，然后移动以下数据。</span><span class="yiyi-st" id="yiyi-1313">另一方面，对<code class="docutils literal"><span class="pre">minor_axis</span></code>的删除操作将非常昂贵。</span><span class="yiyi-st" id="yiyi-1314">在这种情况下，使用<code class="docutils literal"><span class="pre">where</span></code>选择除缺少的数据之外的所有数据。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># returns the number of rows deleted</span>
<span class="gp">In [422]: </span><span class="n">store</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s1">&apos;wp&apos;</span><span class="p">,</span> <span class="s1">&apos;major_axis&gt;20000102&apos;</span> <span class="p">)</span>
<span class="gr">Out[422]: </span><span class="mi">12</span>

<span class="gp">In [423]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;wp&apos;</span><span class="p">)</span>
<span class="gr">Out[423]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 2 (items) x 2 (major_axis) x 4 (minor_axis)</span>
<span class="go">Items axis: Item1 to Item2</span>
<span class="go">Major_axis axis: 2000-01-01 00:00:00 to 2000-01-02 00:00:00</span>
<span class="go">Minor_axis axis: A to D</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1315">警告</span></p>
<p><span class="yiyi-st" id="yiyi-1316">请注意，h5文件中的HDF5 <strong>不会记录空格</strong>。</span><span class="yiyi-st" id="yiyi-1317">因此，重复删除（或删除节点）并再次添加，<strong>将增加文件大小</strong>。</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1318">要<em>重新包装并清除</em>该文件，请使用<a class="reference internal" href="#io-hdf5-ptrepack"><span class="std std-ref">ptrepack</span></a></span></p>
</div>
</div>
<div class="section" id="notes-caveats">
<span id="io-hdf5-notes"></span><h3><span class="yiyi-st" id="yiyi-1319">Notes &amp; Caveats</span></h3>
<div class="section" id="compression">
<h4><span class="yiyi-st" id="yiyi-1320">Compression</span></h4>
<p><span class="yiyi-st" id="yiyi-1321"><code class="docutils literal"><span class="pre">PyTables</span></code>允许压缩存储的数据。</span><span class="yiyi-st" id="yiyi-1322">这适用于各种商店，而不仅仅是表。</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-1323">对于压缩级别（1-9，0是无压缩，默认值），传递<code class="docutils literal"><span class="pre">complevel=int</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-1324">传递<code class="docutils literal"><span class="pre">complib=lib</span></code>其中lib是<code class="docutils literal"><span class="pre">zlib，</span> <span class="pre">bzip2，</span> <span class="pre">lzo，</span> <span class="pre"></span></code>用于您喜欢的压缩库。</span></li>
</ul>
<p><span class="yiyi-st" id="yiyi-1325"><code class="docutils literal"><span class="pre">HDFStore</span></code> will use the file based compression scheme if no overriding <code class="docutils literal"><span class="pre">complib</span></code> or <code class="docutils literal"><span class="pre">complevel</span></code> options are provided. </span><span class="yiyi-st" id="yiyi-1326"><code class="docutils literal"><span class="pre">blosc</span></code>提供非常快速的压缩，是我最常用的。</span><span class="yiyi-st" id="yiyi-1327">请注意，默认情况下可能不安装<code class="docutils literal"><span class="pre">lzo</span></code>和<code class="docutils literal"><span class="pre">bzip2</span></code>（通过Python）。</span></p>
<p><span class="yiyi-st" id="yiyi-1328">压缩文件中的所有对象</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">store_compressed</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">HDFStore</span><span class="p">(</span><span class="s1">&apos;store_compressed.h5&apos;</span><span class="p">,</span> <span class="n">complevel</span><span class="o">=</span><span class="mi">9</span><span class="p">,</span> <span class="n">complib</span><span class="o">=</span><span class="s1">&apos;blosc&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1329">或即时压缩（这只适用于表）。</span><span class="yiyi-st" id="yiyi-1330">您可以通过传递<code class="docutils literal"><span class="pre">complevel=0</span></code>来关闭特定表的文件压缩</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">,</span> <span class="n">df</span><span class="p">,</span> <span class="n">complib</span><span class="o">=</span><span class="s1">&apos;zlib&apos;</span><span class="p">,</span> <span class="n">complevel</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="ptrepack">
<span id="io-hdf5-ptrepack"></span><h4><span class="yiyi-st" id="yiyi-1331">ptrepack</span></h4>
<p><span class="yiyi-st" id="yiyi-1332"><code class="docutils literal"><span class="pre">PyTables</span></code>在写入表之后压缩表时提供更好的写入性能，而不是在开始时打开压缩。</span><span class="yiyi-st" id="yiyi-1333">您可以使用提供的<code class="docutils literal"><span class="pre">PyTables</span></code>实用程序<code class="docutils literal"><span class="pre">ptrepack</span></code>。</span><span class="yiyi-st" id="yiyi-1334">此外，<code class="docutils literal"><span class="pre">ptrepack</span></code>可以在事实之后更改压缩级别。</span></p>
<div class="highlight-console"><div class="highlight"><pre><span></span><span class="go">ptrepack --chunkshape=auto --propindexes --complevel=9 --complib=blosc in.h5 out.h5</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1335">Furthermore <code class="docutils literal"><span class="pre">ptrepack</span> <span class="pre">in.h5</span> <span class="pre">out.h5</span></code> will <em>repack</em> the file to allow you to reuse previously deleted space. </span><span class="yiyi-st" id="yiyi-1336">或者，可以简单地删除文件并再次写入，或使用<code class="docutils literal"><span class="pre">copy</span></code>方法。</span></p>
</div>
<div class="section" id="caveats">
<span id="io-hdf5-caveats"></span><h4><span class="yiyi-st" id="yiyi-1337">Caveats</span></h4>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1338">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1339"><code class="docutils literal"><span class="pre">HDFStore</span></code>是<strong>不能写入</strong>的线程安全。</span><span class="yiyi-st" id="yiyi-1340">底层的<code class="docutils literal"><span class="pre">PyTables</span></code>只支持并发读取（通过线程或进程）。</span><span class="yiyi-st" id="yiyi-1341">如果您需要同时读取和写入<em></em>，则需要在单个进程中在单个线程中将这些操作序列化。</span><span class="yiyi-st" id="yiyi-1342">否则将损坏您的数据。</span><span class="yiyi-st" id="yiyi-1343">有关详细信息，请参阅（<a class="reference external" href="https://github.com/pandas-dev/pandas/issues/2397">GH2397</a>）。</span></p>
</div>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-1344">如果使用锁来管理多个进程之间的写访问，则可能需要在释放写锁之前使用<a class="reference external" href="https://docs.python.org/3/library/os.html#os.fsync" title="(in Python v3.6)"><code class="xref py py-func docutils literal"><span class="pre">fsync()</span></code></a>。</span><span class="yiyi-st" id="yiyi-1345">为方便起见，您可以使用<code class="docutils literal"><span class="pre">store.flush(fsync=True)</span></code>为您执行此操作。</span></li>
<li><span class="yiyi-st" id="yiyi-1346">一旦创建了<code class="docutils literal"><span class="pre">table</span></code>，它的项（Panel）/列（DataFrame）就是固定的；只能附加完全相同的列</span></li>
<li><span class="yiyi-st" id="yiyi-1347">请注意，时区（例如，<code class="docutils literal"><span class="pre">pytz.timezone(&apos;US/Eastern&apos;)</span></code>）在时区版本中不一定相等。</span><span class="yiyi-st" id="yiyi-1348">因此，如果数据被本地化到HDFStore中使用一个版本的时区库的特定时区，并且数据被更新为另一个版本，则数据将被转换为UTC，因为这些时区不被视为相等。</span><span class="yiyi-st" id="yiyi-1349">可以使用相同版本的时区库，也可以使用<code class="docutils literal"><span class="pre">tz_convert</span></code>更新时区定义。</span></li>
</ul>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1350">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1351"><code class="docutils literal"><span class="pre">PyTables</span></code>将显示<code class="docutils literal"><span class="pre">NaturalNameWarning</span></code>。</span><span class="yiyi-st" id="yiyi-1352"><em>自然</em>标识符只包含字母，数字和下划线，且不能以数字开头。</span><span class="yiyi-st" id="yiyi-1353">其他标识符不能在<code class="docutils literal"><span class="pre">where</span></code>子句中使用，通常是一个坏主意。</span></p>
</div>
</div>
</div>
<div class="section" id="datatypes">
<span id="io-hdf5-data-types"></span><h3><span class="yiyi-st" id="yiyi-1354">DataTypes</span></h3>
<p><span class="yiyi-st" id="yiyi-1355"><code class="docutils literal"><span class="pre">HDFStore</span></code>会将对象dtype映射到<code class="docutils literal"><span class="pre">PyTables</span></code>底层dtype。</span><span class="yiyi-st" id="yiyi-1356">这意味着以下类型已知可以工作：</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="68%">
<col width="32%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-1357">类型</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-1358">表示缺少的值</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-1359">浮动：<code class="docutils literal"><span class="pre">float64，</span> <span class="pre">float32，</span> <span class="pre">float16</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-1360"><code class="docutils literal"><span class="pre">np.nan</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-1361">整数：<code class="docutils literal"><span class="pre">int64，</span> <span class="pre">int32，</span> <span class="pre">int8，</span> <span class="pre">uint64，uint32，</span> <span class="pre">uint8  t5 &gt;</span></code></span></td>
<td>&#xA0;</td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-1362">布尔</span></td>
<td>&#xA0;</td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-1363"><code class="docutils literal"><span class="pre">datetime64[ns]</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-1364"><code class="docutils literal"><span class="pre">NaT</span></code></span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-1365"><code class="docutils literal"><span class="pre">timedelta64[ns]</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-1366"><code class="docutils literal"><span class="pre">NaT</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-1367">分类：见下面部分</span></td>
<td>&#xA0;</td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-1368">对象：<code class="docutils literal"><span class="pre">strings</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-1369"><code class="docutils literal"><span class="pre">np.nan</span></code></span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-1370"><code class="docutils literal"><span class="pre">unicode</span></code>列不受支持，<strong>WILL FAIL</strong>。</span></p>
<div class="section" id="categorical-data">
<span id="io-hdf5-categorical"></span><h4><span class="yiyi-st" id="yiyi-1371">Categorical Data</span></h4>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1372"><span class="versionmodified">版本0.15.2中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1373">将数据写入到包含<code class="docutils literal"><span class="pre">category</span></code> dtype的<code class="docutils literal"><span class="pre">HDFStore</span></code>中已在0.15.2中实现。</span><span class="yiyi-st" id="yiyi-1374">查询的工作方式与它是一个对象数组相同。</span><span class="yiyi-st" id="yiyi-1375">但是，<code class="docutils literal"><span class="pre">category</span></code>类型化数据以更有效的方式存储。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [424]: </span><span class="n">dfcat</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span> <span class="s1">&apos;A&apos;</span> <span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;aabbcdba&apos;</span><span class="p">))</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&apos;category&apos;</span><span class="p">),</span>
<span class="gp">   .....:</span>                        <span class="s1">&apos;B&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span> <span class="p">})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [425]: </span><span class="n">dfcat</span>
<span class="gr">Out[425]: </span>
<span class="go">   A         B</span>
<span class="go">0  a  0.603273</span>
<span class="go">1  a  0.262554</span>
<span class="go">2  b -0.979586</span>
<span class="go">3  b  2.132387</span>
<span class="go">4  c  0.892485</span>
<span class="go">5  d  1.996474</span>
<span class="go">6  b  0.231425</span>
<span class="go">7  a  0.980070</span>

<span class="gp">In [426]: </span><span class="n">dfcat</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[426]: </span>
<span class="go">A    category</span>
<span class="go">B     float64</span>
<span class="go">dtype: object</span>

<span class="gp">In [427]: </span><span class="n">cstore</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">HDFStore</span><span class="p">(</span><span class="s1">&apos;cats.h5&apos;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&apos;w&apos;</span><span class="p">)</span>

<span class="gp">In [428]: </span><span class="n">cstore</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;dfcat&apos;</span><span class="p">,</span> <span class="n">dfcat</span><span class="p">,</span> <span class="n">format</span><span class="o">=</span><span class="s1">&apos;table&apos;</span><span class="p">,</span> <span class="n">data_columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">])</span>

<span class="gp">In [429]: </span><span class="n">result</span> <span class="o">=</span> <span class="n">cstore</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;dfcat&apos;</span><span class="p">,</span> <span class="n">where</span><span class="o">=</span><span class="s2">&quot;A in [&apos;b&apos;,&apos;c&apos;]&quot;</span><span class="p">)</span>

<span class="gp">In [430]: </span><span class="n">result</span>
<span class="gr">Out[430]: </span>
<span class="go">   A         B</span>
<span class="go">2  b -0.979586</span>
<span class="go">3  b  2.132387</span>
<span class="go">4  c  0.892485</span>
<span class="go">6  b  0.231425</span>

<span class="gp">In [431]: </span><span class="n">result</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[431]: </span>
<span class="go">A    category</span>
<span class="go">B     float64</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1376">警告</span></p>
<p><span class="yiyi-st" id="yiyi-1377">The format of the <code class="docutils literal"><span class="pre">Categorical</span></code> is readable by prior versions of pandas (&lt; 0.15.2), but will retrieve the data as an integer based column (e.g. the <code class="docutils literal"><span class="pre">codes</span></code>). </span><span class="yiyi-st" id="yiyi-1378">但是，可以检索<code class="docutils literal"><span class="pre">categories</span></code> <em>可以</em>，但需要用户使用显式元路径手动选择它们。</span></p>
<p><span class="yiyi-st" id="yiyi-1379">数据存储如下：</span></p>
<div class="last highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [432]: </span><span class="n">cstore</span>
<span class="gr">Out[432]: </span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: cats.h5</span>
<span class="go">/dfcat                        frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;2,indexers-&gt;[index],dc-&gt;[A])     </span>
<span class="go">/dfcat/meta/A/meta            series_table (typ-&gt;appendable,nrows-&gt;4,ncols-&gt;1,indexers-&gt;[index],dc-&gt;[values])</span>

<span class="c"># to get the categories</span>
<span class="gp">In [433]: </span><span class="n">cstore</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;dfcat/meta/A/meta&apos;</span><span class="p">)</span>
<span class="gr">Out[433]: </span>
<span class="go">0    a</span>
<span class="go">1    b</span>
<span class="go">2    c</span>
<span class="go">3    d</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="string-columns">
<h4><span class="yiyi-st" id="yiyi-1380">String Columns</span></h4>
<p><span class="yiyi-st" id="yiyi-1381"><strong>min_itemsize</strong></span></p>
<p><span class="yiyi-st" id="yiyi-1382"><code class="docutils literal"><span class="pre">HDFStore</span></code>的底层实现对字符串列使用固定列宽（itemsize）。</span><span class="yiyi-st" id="yiyi-1383">字符串列itemsize计算为传递到第一个附加中的<code class="docutils literal"><span class="pre">HDFStore</span></code>，<strong>的数据长度的最大值（对于该列）。</strong></span><span class="yiyi-st" id="yiyi-1384">Subsequent appends, may introduce a string for a column <strong>larger</strong> than the column can hold, an Exception will be raised (otherwise you could have a silent truncation of these columns, leading to loss of information). </span><span class="yiyi-st" id="yiyi-1385">在将来，我们可以放松这一点，并允许发生用户指定的截断。</span></p>
<p><span class="yiyi-st" id="yiyi-1386">将第一个表创建的<code class="docutils literal"><span class="pre">min_itemsize</span></code>传递给先验指定特定字符串列的最小长度。</span><span class="yiyi-st" id="yiyi-1387"><code class="docutils literal"><span class="pre">min_itemsize</span></code>可以是整数，也可以是将列名称映射为整数的dict。</span><span class="yiyi-st" id="yiyi-1388">您可以传递<code class="docutils literal"><span class="pre">values</span></code>作为键，以允许所有<em>可索引项</em>或<em>data_columns</em>具有此min_itemsize。</span></p>
<p><span class="yiyi-st" id="yiyi-1389">从0.11.0开始，传递<code class="docutils literal"><span class="pre">min_itemsize</span></code> dict将使所有传递的列自动创建为<em>data_columns</em>。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1390">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1391">如果您未传递任何<code class="docutils literal"><span class="pre">data_columns</span></code>，则<code class="docutils literal"><span class="pre">min_itemsize</span></code>将是传递的任何字符串的长度的最大值</span></p>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [434]: </span><span class="n">dfs</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">A</span> <span class="o">=</span> <span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="n">B</span> <span class="o">=</span> <span class="s1">&apos;bar&apos;</span><span class="p">),</span><span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">)))</span>

<span class="gp">In [435]: </span><span class="n">dfs</span>
<span class="gr">Out[435]: </span>
<span class="go">     A    B</span>
<span class="go">0  foo  bar</span>
<span class="go">1  foo  bar</span>
<span class="go">2  foo  bar</span>
<span class="go">3  foo  bar</span>
<span class="go">4  foo  bar</span>

<span class="c"># A and B have a size of 30</span>
<span class="gp">In [436]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;dfs&apos;</span><span class="p">,</span> <span class="n">dfs</span><span class="p">,</span> <span class="n">min_itemsize</span> <span class="o">=</span> <span class="mi">30</span><span class="p">)</span>

<span class="gp">In [437]: </span><span class="n">store</span><span class="o">.</span><span class="n">get_storer</span><span class="p">(</span><span class="s1">&apos;dfs&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">table</span>
<span class="gr">Out[437]: </span>
<span class="go">/dfs/table (Table(5,)) &apos;&apos;</span>
<span class="go">  description := {</span>
<span class="go">  &quot;index&quot;: Int64Col(shape=(), dflt=0, pos=0),</span>
<span class="go">  &quot;values_block_0&quot;: StringCol(itemsize=30, shape=(2,), dflt=&apos;&apos;, pos=1)}</span>
<span class="go">  byteorder := &apos;little&apos;</span>
<span class="go">  chunkshape := (963,)</span>
<span class="go">  autoindex := True</span>
<span class="go">  colindexes := {</span>
<span class="go">    &quot;index&quot;: Index(6, medium, shuffle, zlib(1)).is_csi=False}</span>

<span class="c"># A is created as a data_column with a size of 30</span>
<span class="c"># B is size is calculated</span>
<span class="gp">In [438]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;dfs2&apos;</span><span class="p">,</span> <span class="n">dfs</span><span class="p">,</span> <span class="n">min_itemsize</span> <span class="o">=</span> <span class="p">{</span> <span class="s1">&apos;A&apos;</span> <span class="p">:</span> <span class="mi">30</span> <span class="p">})</span>

<span class="gp">In [439]: </span><span class="n">store</span><span class="o">.</span><span class="n">get_storer</span><span class="p">(</span><span class="s1">&apos;dfs2&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">table</span>
<span class="gr">Out[439]: </span>
<span class="go">/dfs2/table (Table(5,)) &apos;&apos;</span>
<span class="go">  description := {</span>
<span class="go">  &quot;index&quot;: Int64Col(shape=(), dflt=0, pos=0),</span>
<span class="go">  &quot;values_block_0&quot;: StringCol(itemsize=3, shape=(1,), dflt=&apos;&apos;, pos=1),</span>
<span class="go">  &quot;A&quot;: StringCol(itemsize=30, shape=(), dflt=&apos;&apos;, pos=2)}</span>
<span class="go">  byteorder := &apos;little&apos;</span>
<span class="go">  chunkshape := (1598,)</span>
<span class="go">  autoindex := True</span>
<span class="go">  colindexes := {</span>
<span class="go">    &quot;A&quot;: Index(6, medium, shuffle, zlib(1)).is_csi=False,</span>
<span class="go">    &quot;index&quot;: Index(6, medium, shuffle, zlib(1)).is_csi=False}</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1392"><strong>nan_rep</strong></span></p>
<p><span class="yiyi-st" id="yiyi-1393">字符串列将使用<code class="docutils literal"><span class="pre">nan_rep</span></code>字符串表示形式将<code class="docutils literal"><span class="pre">np.nan</span></code>（缺少的值）序列化。</span><span class="yiyi-st" id="yiyi-1394">默认为字符串值<code class="docutils literal"><span class="pre">nan</span></code>。</span><span class="yiyi-st" id="yiyi-1395">您可能无意中将实际的<code class="docutils literal"><span class="pre">nan</span></code>值转换为缺失值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [440]: </span><span class="n">dfss</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">A</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span><span class="s1">&apos;bar&apos;</span><span class="p">,</span><span class="s1">&apos;nan&apos;</span><span class="p">]))</span>

<span class="gp">In [441]: </span><span class="n">dfss</span>
<span class="gr">Out[441]: </span>
<span class="go">     A</span>
<span class="go">0  foo</span>
<span class="go">1  bar</span>
<span class="go">2  nan</span>

<span class="gp">In [442]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;dfss&apos;</span><span class="p">,</span> <span class="n">dfss</span><span class="p">)</span>

<span class="gp">In [443]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;dfss&apos;</span><span class="p">)</span>
<span class="gr">Out[443]: </span>
<span class="go">     A</span>
<span class="go">0  foo</span>
<span class="go">1  bar</span>
<span class="go">2  NaN</span>

<span class="c"># here you need to specify a different nan rep</span>
<span class="gp">In [444]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;dfss2&apos;</span><span class="p">,</span> <span class="n">dfss</span><span class="p">,</span> <span class="n">nan_rep</span><span class="o">=</span><span class="s1">&apos;_nan_&apos;</span><span class="p">)</span>

<span class="gp">In [445]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;dfss2&apos;</span><span class="p">)</span>
<span class="gr">Out[445]: </span>
<span class="go">     A</span>
<span class="go">0  foo</span>
<span class="go">1  bar</span>
<span class="go">2  nan</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="external-compatibility">
<span id="io-external-compatibility"></span><h3><span class="yiyi-st" id="yiyi-1396">External Compatibility</span></h3>
<p><span class="yiyi-st" id="yiyi-1397"><code class="docutils literal"><span class="pre">HDFStore</span></code>以特定格式写入<code class="docutils literal"><span class="pre">table</span></code>格式对象，适合生成对pandas对象的无丢失往返。</span><span class="yiyi-st" id="yiyi-1398">为了外部兼容性，<code class="docutils literal"><span class="pre">HDFStore</span></code>可以读取原生<code class="docutils literal"><span class="pre">PyTables</span></code>格式表。</span></p>
<p><span class="yiyi-st" id="yiyi-1399">可以使用<code class="docutils literal"><span class="pre">rhdf5</span></code>库（<a class="reference external" href="http://www.bioconductor.org/packages/release/bioc/html/rhdf5.html">包网站</a>）将可以轻松导入<code class="docutils literal"><span class="pre">R</span></code>的<code class="docutils literal"><span class="pre">HDFStore</span></code> ）。</span><span class="yiyi-st" id="yiyi-1400">创建一个表格式存储像这样：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [446]: </span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

<span class="gp">In [447]: </span><span class="n">df_for_r</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s2">&quot;first&quot;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">),</span>
<span class="gp">   .....:</span>                          <span class="s2">&quot;second&quot;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">100</span><span class="p">),</span>
<span class="gp">   .....:</span>                          <span class="s2">&quot;class&quot;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="mi">100</span><span class="p">,))},</span>
<span class="gp">   .....:</span>                          <span class="n">index</span><span class="o">=</span><span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">))</span>
<span class="gp">   .....:</span> 

<span class="gp">In [448]: </span><span class="n">df_for_r</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
<span class="gr">Out[448]: </span>
<span class="go">   class     first    second</span>
<span class="go">0      0  0.417022  0.326645</span>
<span class="go">1      0  0.720324  0.527058</span>
<span class="go">2      1  0.000114  0.885942</span>
<span class="go">3      1  0.302333  0.357270</span>
<span class="go">4      1  0.146756  0.908535</span>

<span class="gp">In [449]: </span><span class="n">store_export</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">HDFStore</span><span class="p">(</span><span class="s1">&apos;export.h5&apos;</span><span class="p">)</span>

<span class="gp">In [450]: </span><span class="n">store_export</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;df_for_r&apos;</span><span class="p">,</span> <span class="n">df_for_r</span><span class="p">,</span> <span class="n">data_columns</span><span class="o">=</span><span class="n">df_dc</span><span class="o">.</span><span class="n">columns</span><span class="p">)</span>

<span class="gp">In [451]: </span><span class="n">store_export</span>
<span class="gr">Out[451]: </span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: export.h5</span>
<span class="go">/df_for_r            frame_table  (typ-&gt;appendable,nrows-&gt;100,ncols-&gt;3,indexers-&gt;[index])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1401">在R中，这个文件可以使用<code class="docutils literal"><span class="pre">rhdf5</span></code>库读入<code class="docutils literal"><span class="pre">data.frame</span></code>对象。</span><span class="yiyi-st" id="yiyi-1402">以下示例函数从值读取相应的列名称和数据值，并将它们组合到<code class="docutils literal"><span class="pre">data.frame</span></code>中：</span></p>
<div class="highlight-R"><div class="highlight"><pre><span></span><span class="c1"># Load values and column names for all datasets from corresponding nodes and</span>
<span class="c1"># insert them into one data.frame object.</span>

<span class="kn">library</span><span class="p">(</span>rhdf5<span class="p">)</span>

loadhdf5data <span class="o">&lt;-</span> <span class="kr">function</span><span class="p">(</span>h5File<span class="p">)</span> <span class="p">{</span>

listing <span class="o">&lt;-</span> h5ls<span class="p">(</span>h5File<span class="p">)</span>
<span class="c1"># Find all data nodes, values are stored in *_values and corresponding column</span>
<span class="c1"># titles in *_items</span>
data_nodes <span class="o">&lt;-</span> <span class="kp">grep</span><span class="p">(</span><span class="s">&quot;_values&quot;</span><span class="p">,</span> listing<span class="o">$</span>name<span class="p">)</span>
name_nodes <span class="o">&lt;-</span> <span class="kp">grep</span><span class="p">(</span><span class="s">&quot;_items&quot;</span><span class="p">,</span> listing<span class="o">$</span>name<span class="p">)</span>
data_paths <span class="o">=</span> <span class="kp">paste</span><span class="p">(</span>listing<span class="o">$</span>group<span class="p">[</span>data_nodes<span class="p">],</span> listing<span class="o">$</span>name<span class="p">[</span>data_nodes<span class="p">],</span> sep <span class="o">=</span> <span class="s">&quot;/&quot;</span><span class="p">)</span>
name_paths <span class="o">=</span> <span class="kp">paste</span><span class="p">(</span>listing<span class="o">$</span>group<span class="p">[</span>name_nodes<span class="p">],</span> listing<span class="o">$</span>name<span class="p">[</span>name_nodes<span class="p">],</span> sep <span class="o">=</span> <span class="s">&quot;/&quot;</span><span class="p">)</span>
columns <span class="o">=</span> <span class="kt">list</span><span class="p">()</span>
<span class="kr">for</span> <span class="p">(</span>idx <span class="kr">in</span> <span class="kp">seq</span><span class="p">(</span>data_paths<span class="p">))</span> <span class="p">{</span>
  <span class="c1"># NOTE: matrices returned by h5read have to be transposed to to obtain</span>
  <span class="c1"># required Fortran order!</span>
  data <span class="o">&lt;-</span> <span class="kt">data.frame</span><span class="p">(</span><span class="kp">t</span><span class="p">(</span>h5read<span class="p">(</span>h5File<span class="p">,</span> data_paths<span class="p">[</span>idx<span class="p">])))</span>
  names <span class="o">&lt;-</span> <span class="kp">t</span><span class="p">(</span>h5read<span class="p">(</span>h5File<span class="p">,</span> name_paths<span class="p">[</span>idx<span class="p">]))</span>
  entry <span class="o">&lt;-</span> <span class="kt">data.frame</span><span class="p">(</span>data<span class="p">)</span>
  <span class="kp">colnames</span><span class="p">(</span>entry<span class="p">)</span> <span class="o">&lt;-</span> <span class="kp">names</span>
  columns <span class="o">&lt;-</span> <span class="kp">append</span><span class="p">(</span>columns<span class="p">,</span> entry<span class="p">)</span>
<span class="p">}</span>

data <span class="o">&lt;-</span> <span class="kt">data.frame</span><span class="p">(</span>columns<span class="p">)</span>

<span class="kr">return</span><span class="p">(</span>data<span class="p">)</span>
<span class="p">}</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1403">现在您可以将<code class="docutils literal"><span class="pre">DataFrame</span></code>导入R：</span></p>
<div class="highlight-R"><div class="highlight"><pre><span></span><span class="o">&gt;</span> data <span class="o">=</span> loadhdf5data<span class="p">(</span><span class="s">&quot;transfer.hdf5&quot;</span><span class="p">)</span>
<span class="o">&gt;</span> <span class="kp">head</span><span class="p">(</span>data<span class="p">)</span>
         first    second <span class="kp">class</span>
<span class="m">1</span> <span class="m">0.4170220047</span> <span class="m">0.3266449</span>     <span class="m">0</span>
<span class="m">2</span> <span class="m">0.7203244934</span> <span class="m">0.5270581</span>     <span class="m">0</span>
<span class="m">3</span> <span class="m">0.0001143748</span> <span class="m">0.8859421</span>     <span class="m">1</span>
<span class="m">4</span> <span class="m">0.3023325726</span> <span class="m">0.3572698</span>     <span class="m">1</span>
<span class="m">5</span> <span class="m">0.1467558908</span> <span class="m">0.9085352</span>     <span class="m">1</span>
<span class="m">6</span> <span class="m">0.0923385948</span> <span class="m">0.6233601</span>     <span class="m">1</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1404">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1405">R函数列出整个HDF5文件的内容，并从所有匹配的节点组装<code class="docutils literal"><span class="pre">data.frame</span></code>对象，因此，如果您已存储多个<code class="docutils literal"><span class="pre">DataFrame</span></code>对象到一个单一的HDF5文件。</span></p>
</div>
</div>
<div class="section" id="backwards-compatibility">
<h3><span class="yiyi-st" id="yiyi-1406">Backwards Compatibility</span></h3>
<p><span class="yiyi-st" id="yiyi-1407"><code class="docutils literal"><span class="pre">HDFStore</span></code>的0.10.1可读取在先前版本的pandas中创建的表，但不支持使用之前（未记录）方法的查询术语。</span><span class="yiyi-st" id="yiyi-1408"><code class="docutils literal"><span class="pre">HDFStore</span></code>将会发出警告。</span><span class="yiyi-st" id="yiyi-1409">您必须读取整个文件，并使用新格式写出，使用方法<code class="docutils literal"><span class="pre">copy</span></code>以利用更新。</span><span class="yiyi-st" id="yiyi-1410">组属性<code class="docutils literal"><span class="pre">pandas_version</span></code>包含版本信息。</span><span class="yiyi-st" id="yiyi-1411"><code class="docutils literal"><span class="pre">copy</span></code>需要一些选项，请参阅docstring。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># a legacy store</span>
<span class="gp">In [452]: </span><span class="n">legacy_store</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">HDFStore</span><span class="p">(</span><span class="n">legacy_file_path</span><span class="p">,</span><span class="s1">&apos;r&apos;</span><span class="p">)</span>

<span class="gp">In [453]: </span><span class="n">legacy_store</span>
<span class="gr">Out[453]: </span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: /home/joris/scipy/pandas/doc/source/_static/legacy_0.10.h5</span>
<span class="go">/a                    series       (shape-&gt;[30])                                                                        </span>
<span class="go">/b                    frame        (shape-&gt;[30,4])                                                                      </span>
<span class="go">/df1_mixed            frame_table [0.10.0] (typ-&gt;appendable,nrows-&gt;30,ncols-&gt;11,indexers-&gt;[index])                      </span>
<span class="go">/foo/bar              wide         (shape-&gt;[3,30,4])                                                                    </span>
<span class="go">/p1_mixed             wide_table  [0.10.0] (typ-&gt;appendable,nrows-&gt;120,ncols-&gt;9,indexers-&gt;[major_axis,minor_axis])      </span>
<span class="go">/p4d_mixed            ndim_table  [0.10.0] (typ-&gt;appendable,nrows-&gt;360,ncols-&gt;9,indexers-&gt;[items,major_axis,minor_axis])</span>

<span class="c"># copy (and return the new handle)</span>
<span class="gp">In [454]: </span><span class="n">new_store</span> <span class="o">=</span> <span class="n">legacy_store</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="s1">&apos;store_new.h5&apos;</span><span class="p">)</span>

<span class="gp">In [455]: </span><span class="n">new_store</span>
<span class="gr">Out[455]: </span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: store_new.h5</span>
<span class="go">/a                    series       (shape-&gt;[30])                                                                </span>
<span class="go">/b                    frame        (shape-&gt;[30,4])                                                              </span>
<span class="go">/df1_mixed            frame_table  (typ-&gt;appendable,nrows-&gt;30,ncols-&gt;11,indexers-&gt;[index])                      </span>
<span class="go">/foo/bar              wide         (shape-&gt;[3,30,4])                                                            </span>
<span class="go">/p1_mixed             wide_table   (typ-&gt;appendable,nrows-&gt;120,ncols-&gt;9,indexers-&gt;[major_axis,minor_axis])      </span>
<span class="go">/p4d_mixed            wide_table   (typ-&gt;appendable,nrows-&gt;360,ncols-&gt;9,indexers-&gt;[items,major_axis,minor_axis])</span>

<span class="gp">In [456]: </span><span class="n">new_store</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="performance">
<h3><span class="yiyi-st" id="yiyi-1412">Performance</span></h3>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-1413">与<code class="docutils literal"><span class="pre">fixed</span></code>存储相比，<code class="docutils literal"><span class="pre">tables</span></code>格式具有写入性能损失。</span><span class="yiyi-st" id="yiyi-1414">好处是附加/删除和查询（可能非常大量的数据）的能力。</span><span class="yiyi-st" id="yiyi-1415">与普通商店相比，写入时间通常更长。</span><span class="yiyi-st" id="yiyi-1416">查询时间可以很快，特别是在索引轴上。</span></li>
<li><span class="yiyi-st" id="yiyi-1417">您可以将<code class="docutils literal"><span class="pre">chunksize=&lt;int&gt;</span></code>传递到<code class="docutils literal"><span class="pre">append</span></code>，指定写入chunksize（默认值为50000）。</span><span class="yiyi-st" id="yiyi-1418">这将大大降低写入时的内存使用率。</span></li>
<li><span class="yiyi-st" id="yiyi-1419">您可以将<code class="docutils literal"><span class="pre">expectedrows=&lt;int&gt;</span></code>传递到第一个<code class="docutils literal"><span class="pre">append</span></code>，以设置<code class="docutils literal"><span class="pre">PyTables</span></code>预期的预期行数TOTAL。</span><span class="yiyi-st" id="yiyi-1420">这将优化读/写性能。</span></li>
<li><span class="yiyi-st" id="yiyi-1421">重复的行可以写入表，但在选择中被过滤掉（最后一个项被选择；因此，一个表在主要，次要对上是唯一的）</span></li>
<li><span class="yiyi-st" id="yiyi-1422">如果您尝试存储将由PyTables（而不是存储为地方性类型）选择的类型，将会引发<code class="docutils literal"><span class="pre">PerformanceWarning</span></code>。</span><span class="yiyi-st" id="yiyi-1423">有关详细信息和一些解决方案，请参阅<a class="reference external" href="http://stackoverflow.com/questions/14355151/how-to-make-pandas-hdfstore-put-operation-faster/14370190#14370190">此处</a>。</span></li>
</ul>
</div>
<div class="section" id="experimental">
<h3><span class="yiyi-st" id="yiyi-1424">Experimental</span></h3>
<p><span class="yiyi-st" id="yiyi-1425">HDFStore支持<code class="docutils literal"><span class="pre">Panel4D</span></code>存储。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [457]: </span><span class="n">p4d</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Panel4D</span><span class="p">({</span> <span class="s1">&apos;l1&apos;</span> <span class="p">:</span> <span class="n">wp</span> <span class="p">})</span>

<span class="gp">In [458]: </span><span class="n">p4d</span>
<span class="gr">Out[458]: </span>
<span class="go">&lt;class &apos;pandas.core.panelnd.Panel4D&apos;&gt;</span>
<span class="go">Dimensions: 1 (labels) x 2 (items) x 5 (major_axis) x 4 (minor_axis)</span>
<span class="go">Labels axis: l1 to l1</span>
<span class="go">Items axis: Item1 to Item2</span>
<span class="go">Major_axis axis: 2000-01-01 00:00:00 to 2000-01-05 00:00:00</span>
<span class="go">Minor_axis axis: A to D</span>

<span class="gp">In [459]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;p4d&apos;</span><span class="p">,</span> <span class="n">p4d</span><span class="p">)</span>

<span class="gp">In [460]: </span><span class="n">store</span>
<span class="gr">Out[460]: </span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: store.h5</span>
<span class="go">/df                     frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;3,indexers-&gt;[index])                         </span>
<span class="go">/df1_mt                 frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;2,indexers-&gt;[index],dc-&gt;[A,B])               </span>
<span class="go">/df2_mt                 frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;5,indexers-&gt;[index])                         </span>
<span class="go">/df_coord               frame_table  (typ-&gt;appendable,nrows-&gt;1000,ncols-&gt;2,indexers-&gt;[index])                      </span>
<span class="go">/df_dc                  frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;5,indexers-&gt;[index],dc-&gt;[B,C,string,string2])</span>
<span class="go">/df_mask                frame_table  (typ-&gt;appendable,nrows-&gt;1000,ncols-&gt;2,indexers-&gt;[index])                      </span>
<span class="go">/df_mi                  frame_table  (typ-&gt;appendable_multi,nrows-&gt;10,ncols-&gt;5,indexers-&gt;[index],dc-&gt;[bar,foo])    </span>
<span class="go">/df_mixed               frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;7,indexers-&gt;[index])                         </span>
<span class="go">/dfeq                   frame_table  (typ-&gt;appendable,nrows-&gt;10,ncols-&gt;1,indexers-&gt;[index],dc-&gt;[number])           </span>
<span class="go">/dfq                    frame_table  (typ-&gt;appendable,nrows-&gt;10,ncols-&gt;4,indexers-&gt;[index],dc-&gt;[A,B,C,D])          </span>
<span class="go">/dfs                    frame_table  (typ-&gt;appendable,nrows-&gt;5,ncols-&gt;2,indexers-&gt;[index])                         </span>
<span class="go">/dfs2                   frame_table  (typ-&gt;appendable,nrows-&gt;5,ncols-&gt;2,indexers-&gt;[index],dc-&gt;[A])                 </span>
<span class="go">/dfss                   frame_table  (typ-&gt;appendable,nrows-&gt;3,ncols-&gt;1,indexers-&gt;[index])                         </span>
<span class="go">/dfss2                  frame_table  (typ-&gt;appendable,nrows-&gt;3,ncols-&gt;1,indexers-&gt;[index])                         </span>
<span class="go">/dftd                   frame_table  (typ-&gt;appendable,nrows-&gt;10,ncols-&gt;3,indexers-&gt;[index],dc-&gt;[A,B,C])            </span>
<span class="go">/foo/bar/bah            frame        (shape-&gt;[8,3])                                                                </span>
<span class="go">/p4d                    wide_table   (typ-&gt;appendable,nrows-&gt;40,ncols-&gt;1,indexers-&gt;[items,major_axis,minor_axis])  </span>
<span class="go">/wp                     wide_table   (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;2,indexers-&gt;[major_axis,minor_axis])         </span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1426">默认情况下，这些索引为三个轴<code class="docutils literal"><span class="pre">项，</span> <span class="pre">major_axis，</span> <span class="pre">minor_axis</span></code>。</span><span class="yiyi-st" id="yiyi-1427">在<code class="docutils literal"><span class="pre">AppendableTable</span></code>上，可以使用第一个追加设置不同的索引方案，具体取决于您要如何存储数据。</span><span class="yiyi-st" id="yiyi-1428">将<code class="docutils literal"><span class="pre">axes</span></code>关键字与尺寸列表（当前必须比对象的总尺寸小1）相关联。</span><span class="yiyi-st" id="yiyi-1429">这在创建表之后不能更改。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [461]: </span><span class="n">store</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&apos;p4d2&apos;</span><span class="p">,</span> <span class="n">p4d</span><span class="p">,</span> <span class="n">axes</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;labels&apos;</span><span class="p">,</span> <span class="s1">&apos;major_axis&apos;</span><span class="p">,</span> <span class="s1">&apos;minor_axis&apos;</span><span class="p">])</span>

<span class="gp">In [462]: </span><span class="n">store</span>
<span class="gr">Out[462]: </span>
<span class="go">&lt;class &apos;pandas.io.pytables.HDFStore&apos;&gt;</span>
<span class="go">File path: store.h5</span>
<span class="go">/df                     frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;3,indexers-&gt;[index])                         </span>
<span class="go">/df1_mt                 frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;2,indexers-&gt;[index],dc-&gt;[A,B])               </span>
<span class="go">/df2_mt                 frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;5,indexers-&gt;[index])                         </span>
<span class="go">/df_coord               frame_table  (typ-&gt;appendable,nrows-&gt;1000,ncols-&gt;2,indexers-&gt;[index])                      </span>
<span class="go">/df_dc                  frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;5,indexers-&gt;[index],dc-&gt;[B,C,string,string2])</span>
<span class="go">/df_mask                frame_table  (typ-&gt;appendable,nrows-&gt;1000,ncols-&gt;2,indexers-&gt;[index])                      </span>
<span class="go">/df_mi                  frame_table  (typ-&gt;appendable_multi,nrows-&gt;10,ncols-&gt;5,indexers-&gt;[index],dc-&gt;[bar,foo])    </span>
<span class="go">/df_mixed               frame_table  (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;7,indexers-&gt;[index])                         </span>
<span class="go">/dfeq                   frame_table  (typ-&gt;appendable,nrows-&gt;10,ncols-&gt;1,indexers-&gt;[index],dc-&gt;[number])           </span>
<span class="go">/dfq                    frame_table  (typ-&gt;appendable,nrows-&gt;10,ncols-&gt;4,indexers-&gt;[index],dc-&gt;[A,B,C,D])          </span>
<span class="go">/dfs                    frame_table  (typ-&gt;appendable,nrows-&gt;5,ncols-&gt;2,indexers-&gt;[index])                         </span>
<span class="go">/dfs2                   frame_table  (typ-&gt;appendable,nrows-&gt;5,ncols-&gt;2,indexers-&gt;[index],dc-&gt;[A])                 </span>
<span class="go">/dfss                   frame_table  (typ-&gt;appendable,nrows-&gt;3,ncols-&gt;1,indexers-&gt;[index])                         </span>
<span class="go">/dfss2                  frame_table  (typ-&gt;appendable,nrows-&gt;3,ncols-&gt;1,indexers-&gt;[index])                         </span>
<span class="go">/dftd                   frame_table  (typ-&gt;appendable,nrows-&gt;10,ncols-&gt;3,indexers-&gt;[index],dc-&gt;[A,B,C])            </span>
<span class="go">/foo/bar/bah            frame        (shape-&gt;[8,3])                                                                </span>
<span class="go">/p4d                    wide_table   (typ-&gt;appendable,nrows-&gt;40,ncols-&gt;1,indexers-&gt;[items,major_axis,minor_axis])  </span>
<span class="go">/p4d2                   wide_table   (typ-&gt;appendable,nrows-&gt;20,ncols-&gt;2,indexers-&gt;[labels,major_axis,minor_axis]) </span>
<span class="go">/wp                     wide_table   (typ-&gt;appendable,nrows-&gt;8,ncols-&gt;2,indexers-&gt;[major_axis,minor_axis])         </span>

<span class="gp">In [463]: </span><span class="n">store</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&apos;p4d2&apos;</span><span class="p">,</span> <span class="p">[</span> <span class="n">pd</span><span class="o">.</span><span class="n">Term</span><span class="p">(</span><span class="s1">&apos;labels=l1&apos;</span><span class="p">),</span> <span class="n">pd</span><span class="o">.</span><span class="n">Term</span><span class="p">(</span><span class="s1">&apos;items=Item1&apos;</span><span class="p">),</span> <span class="n">pd</span><span class="o">.</span><span class="n">Term</span><span class="p">(</span><span class="s1">&apos;minor_axis=A_big_strings&apos;</span><span class="p">)</span> <span class="p">])</span>
<span class="gr">Out[463]: </span>
<span class="go">&lt;class &apos;pandas.core.panelnd.Panel4D&apos;&gt;</span>
<span class="go">Dimensions: 0 (labels) x 1 (items) x 0 (major_axis) x 0 (minor_axis)</span>
<span class="go">Labels axis: None</span>
<span class="go">Items axis: Item1 to Item1</span>
<span class="go">Major_axis axis: None</span>
<span class="go">Minor_axis axis: None</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="sql-queries">
<span id="io-sql"></span><h2><span class="yiyi-st" id="yiyi-1430">SQL Queries</span></h2>
<p><span class="yiyi-st" id="yiyi-1431"><code class="xref py py-mod docutils literal"><span class="pre">pandas.io.sql</span></code>模块提供了一组查询包装器，以便于数据检索和减少对特定于数据库的API的依赖。</span><span class="yiyi-st" id="yiyi-1432">数据库抽象由SQLAlchemy（如果已安装）提供。</span><span class="yiyi-st" id="yiyi-1433">此外，您将需要一个用于数据库的驱动程序库。</span><span class="yiyi-st" id="yiyi-1434">这种驱动程序的示例是PostgreSQL的<a class="reference external" href="http://initd.org/psycopg/">psycopg2</a>或MySQL的<a class="reference external" href="https://github.com/PyMySQL/PyMySQL">pymysql</a>。</span><span class="yiyi-st" id="yiyi-1435">对于<a class="reference external" href="https://docs.python.org/3.5/library/sqlite3.html">SQLite</a>，默认情况下包含在Python的标准库中。</span><span class="yiyi-st" id="yiyi-1436">您可以在<a class="reference external" href="http://docs.sqlalchemy.org/en/latest/dialects/index.html">SQLAlchemy docs</a>中找到每种SQL方言的支持的驱动程序的概述。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1437"><span class="versionmodified">版本0.14.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1438">如果未安装SQLAlchemy，则仅为sqlite提供回退（对于mysql，为向后兼容性，但不推荐使用，将在以后的版本中删除）。</span><span class="yiyi-st" id="yiyi-1439">此模式需要一个参考<a class="reference external" href="http://www.python.org/dev/peps/pep-0249/">Python DB-API</a>的Python数据库适配器。</span></p>
<p><span class="yiyi-st" id="yiyi-1440">有关一些高级策略，另见一些<a class="reference internal" href="cookbook.html#cookbook-sql"><span class="std std-ref">cookbook examples</span></a>。</span></p>
<p><span class="yiyi-st" id="yiyi-1441">主要功能有：</span></p>
<table border="1" class="longtable docutils">
<colgroup>
<col width="10%">
<col width="90%">
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-1442"><a class="reference internal" href="generated/pandas.read_sql_table.html#pandas.read_sql_table" title="pandas.read_sql_table"><code class="xref py py-obj docutils literal"><span class="pre">read_sql_table</span></code></a>（table_name，con [，schema，...]）</span></td>
<td><span class="yiyi-st" id="yiyi-1443">将SQL数据库表读入DataFrame。</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-1444"><a class="reference internal" href="generated/pandas.read_sql_query.html#pandas.read_sql_query" title="pandas.read_sql_query"><code class="xref py py-obj docutils literal"><span class="pre">read_sql_query</span></code></a>（sql，con [，index_col，...]）</span></td>
<td><span class="yiyi-st" id="yiyi-1445">将SQL查询读入DataFrame。</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-1446"><a class="reference internal" href="generated/pandas.read_sql.html#pandas.read_sql" title="pandas.read_sql"><code class="xref py py-obj docutils literal"><span class="pre">read_sql</span></code></a>（sql，con [，index_col，...]）</span></td>
<td><span class="yiyi-st" id="yiyi-1447">将SQL查询或数据库表读入DataFrame。</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-1448"><a class="reference internal" href="generated/pandas.DataFrame.to_sql.html#pandas.DataFrame.to_sql" title="pandas.DataFrame.to_sql"><code class="xref py py-obj docutils literal"><span class="pre">DataFrame.to_sql</span></code></a>（name，con [，flavor，...]）</span></td>
<td><span class="yiyi-st" id="yiyi-1449">将存储在DataFrame中的记录写入SQL数据库。</span></td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1450">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1451">函数<a class="reference internal" href="generated/pandas.read_sql.html#pandas.read_sql" title="pandas.read_sql"><code class="xref py py-func docutils literal"><span class="pre">read_sql()</span></code></a>是围绕<a class="reference internal" href="generated/pandas.read_sql_table.html#pandas.read_sql_table" title="pandas.read_sql_table"><code class="xref py py-func docutils literal"><span class="pre">read_sql_table()</span></code></a>和<a class="reference internal" href="generated/pandas.read_sql_query.html#pandas.read_sql_query" title="pandas.read_sql_query"><code class="xref py py-func docutils literal"><span class="pre">read_sql_query()</span></code></a>（以及为了向后兼容）的方便包装，取决于提供的输入（数据库表名或sql查询）。</span><span class="yiyi-st" id="yiyi-1452">如果表名称具有特殊字符，则不需要引用。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1453">在以下示例中，我们使用<a class="reference external" href="http://www.sqlite.org/">SQlite</a> SQL数据库引擎。</span><span class="yiyi-st" id="yiyi-1454">您可以使用临时SQLite数据库，其中数据存储在“内存”中。</span></p>
<p><span class="yiyi-st" id="yiyi-1455">要使用SQLAlchemy连接，可以使用<code class="xref py py-func docutils literal"><span class="pre">create_engine()</span></code>函数从数据库URI创建引擎对象。</span><span class="yiyi-st" id="yiyi-1456">每个要连接的数据库只需创建引擎一次。</span><span class="yiyi-st" id="yiyi-1457">有关<code class="xref py py-func docutils literal"><span class="pre">create_engine()</span></code>和URI格式的更多信息，请参阅下面的示例和SQLAlchemy <a class="reference external" href="http://docs.sqlalchemy.org/en/latest/core/engines.html">文档</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [464]: </span><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">create_engine</span>

<span class="c"># Create your engine.</span>
<span class="gp">In [465]: </span><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">&apos;sqlite:///:memory:&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1458"></span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">engine</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span> <span class="k">as</span> <span class="n">conn</span><span class="p">,</span> <span class="n">conn</span><span class="o">.</span><span class="n">begin</span><span class="p">():</span>
    <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_sql_table</span><span class="p">(</span><span class="s1">&apos;data&apos;</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="writing-dataframes">
<h3><span class="yiyi-st" id="yiyi-1459">Writing DataFrames</span></h3>
<p><span class="yiyi-st" id="yiyi-1460">假设以下数据在DataFrame <code class="docutils literal"><span class="pre">data</span></code>中，我们可以使用<a class="reference internal" href="generated/pandas.DataFrame.to_sql.html#pandas.DataFrame.to_sql" title="pandas.DataFrame.to_sql"><code class="xref py py-func docutils literal"><span class="pre">to_sql()</span></code></a>将其插入数据库。</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="13%">
<col width="32%">
<col width="18%">
<col width="18%">
<col width="18%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-1461">ID</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-1462">日期</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-1463">Col_1</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-1464">Col_2</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-1465">Col_3</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-1466">26</span></td>
<td><span class="yiyi-st" id="yiyi-1467">2012-10-18</span></td>
<td><span class="yiyi-st" id="yiyi-1468">X</span></td>
<td><span class="yiyi-st" id="yiyi-1469">25.7</span></td>
<td><span class="yiyi-st" id="yiyi-1470">真正</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-1471">42</span></td>
<td><span class="yiyi-st" id="yiyi-1472">2012-10-19</span></td>
<td><span class="yiyi-st" id="yiyi-1473">Y</span></td>
<td><span class="yiyi-st" id="yiyi-1474">-12.4</span></td>
<td><span class="yiyi-st" id="yiyi-1475">假</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-1476">63</span></td>
<td><span class="yiyi-st" id="yiyi-1477">2012-10-20</span></td>
<td><span class="yiyi-st" id="yiyi-1478">Z</span></td>
<td><span class="yiyi-st" id="yiyi-1479">5.73</span></td>
<td><span class="yiyi-st" id="yiyi-1480">真正</span></td>
</tr>
</tbody>
</table>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [466]: </span><span class="n">data</span><span class="o">.</span><span class="n">to_sql</span><span class="p">(</span><span class="s1">&apos;data&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1481">对于某些数据库，由于超过了数据包大小限制，写入大型DataFrames可能会导致错误。</span><span class="yiyi-st" id="yiyi-1482">这可以通过在调用<code class="docutils literal"><span class="pre">to_sql</span></code>时设置<code class="docutils literal"><span class="pre">chunksize</span></code>参数来避免。</span><span class="yiyi-st" id="yiyi-1483">例如，以下将<code class="docutils literal"><span class="pre">data</span></code>以批处理形式批量写入数据库：每次1000行：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [467]: </span><span class="n">data</span><span class="o">.</span><span class="n">to_sql</span><span class="p">(</span><span class="s1">&apos;data_chunked&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">,</span> <span class="n">chunksize</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="sql-data-types">
<h4><span class="yiyi-st" id="yiyi-1484">SQL data types</span></h4>
<p><span class="yiyi-st" id="yiyi-1485"><a class="reference internal" href="generated/pandas.DataFrame.to_sql.html#pandas.DataFrame.to_sql" title="pandas.DataFrame.to_sql"><code class="xref py py-func docutils literal"><span class="pre">to_sql()</span></code></a>将尝试根据数据的dtype将数据映射到适当的SQL数据类型。</span><span class="yiyi-st" id="yiyi-1486">当您有dtype <code class="docutils literal"><span class="pre">object</span></code>的列时，pandas将尝试推断数据类型。</span></p>
<p><span class="yiyi-st" id="yiyi-1487">您可以通过使用<code class="docutils literal"><span class="pre">dtype</span></code>参数指定任何列的所需SQL类型来覆盖默认类型。</span><span class="yiyi-st" id="yiyi-1488">此参数需要一个字典将列名称映射到SQLAlchemy类型（或sqlite3回退模式的字符串）。</span><span class="yiyi-st" id="yiyi-1489">例如，指定对字符串列使用sqlalchemy <code class="docutils literal"><span class="pre">String</span></code>类型而不是默认的<code class="docutils literal"><span class="pre">Text</span></code>类型：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [468]: </span><span class="kn">from</span> <span class="nn">sqlalchemy.types</span> <span class="kn">import</span> <span class="n">String</span>

<span class="gp">In [469]: </span><span class="n">data</span><span class="o">.</span><span class="n">to_sql</span><span class="p">(</span><span class="s1">&apos;data_dtype&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;Col_1&apos;</span><span class="p">:</span> <span class="n">String</span><span class="p">})</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1490">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1491">由于timedelta在不同数据库风格中的支持有限，因此类型为<code class="docutils literal"><span class="pre">timedelta64</span></code>的列将作为整数值写入数据库中，并将生成警告。</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1492">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1493"><code class="docutils literal"><span class="pre">category</span></code> dtype的列将转换为密集表示，如您在<code class="docutils literal"><span class="pre">np.asarray(categorical)</span></code>（例如，对于字符串类别，这将提供一个字符串数组）。</span><span class="yiyi-st" id="yiyi-1494">因此，读取数据库表时，<strong>不会</strong>生成分类。</span></p>
</div>
</div>
</div>
<div class="section" id="reading-tables">
<h3><span class="yiyi-st" id="yiyi-1495">Reading Tables</span></h3>
<p><span class="yiyi-st" id="yiyi-1496"><a class="reference internal" href="generated/pandas.read_sql_table.html#pandas.read_sql_table" title="pandas.read_sql_table"><code class="xref py py-func docutils literal"><span class="pre">read_sql_table()</span></code></a>将读取给定表名称和可选的要读取的列子集的数据库表。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1497">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1498">要使用<a class="reference internal" href="generated/pandas.read_sql_table.html#pandas.read_sql_table" title="pandas.read_sql_table"><code class="xref py py-func docutils literal"><span class="pre">read_sql_table()</span></code></a>，您<strong>必须</strong>安装SQLAlchemy可选依赖项。</span></p>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [470]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_sql_table</span><span class="p">(</span><span class="s1">&apos;data&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">)</span>
<span class="gr">Out[470]: </span>
<span class="go">   index  id       Date Col_1  Col_2  Col_3</span>
<span class="go">0      0  26 2010-10-18     X  27.50   True</span>
<span class="go">1      1  42 2010-10-19     Y -12.50  False</span>
<span class="go">2      2  63 2010-10-20     Z   5.73   True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1499">您还可以将列的名称指定为DataFrame索引，并指定要读取的列的子集。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [471]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_sql_table</span><span class="p">(</span><span class="s1">&apos;data&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="s1">&apos;id&apos;</span><span class="p">)</span>
<span class="gr">Out[471]: </span>
<span class="go">    index       Date Col_1  Col_2  Col_3</span>
<span class="go">id                                      </span>
<span class="go">26      0 2010-10-18     X  27.50   True</span>
<span class="go">42      1 2010-10-19     Y -12.50  False</span>
<span class="go">63      2 2010-10-20     Z   5.73   True</span>

<span class="gp">In [472]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_sql_table</span><span class="p">(</span><span class="s1">&apos;data&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;Col_1&apos;</span><span class="p">,</span> <span class="s1">&apos;Col_2&apos;</span><span class="p">])</span>
<span class="gr">Out[472]: </span>
<span class="go">  Col_1  Col_2</span>
<span class="go">0     X  27.50</span>
<span class="go">1     Y -12.50</span>
<span class="go">2     Z   5.73</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1500">您可以明确强制将列解析为日期：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [473]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_sql_table</span><span class="p">(</span><span class="s1">&apos;data&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;Date&apos;</span><span class="p">])</span>
<span class="gr">Out[473]: </span>
<span class="go">   index  id       Date Col_1  Col_2  Col_3</span>
<span class="go">0      0  26 2010-10-18     X  27.50   True</span>
<span class="go">1      1  42 2010-10-19     Y -12.50  False</span>
<span class="go">2      2  63 2010-10-20     Z   5.73   True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1501">如果需要，您可以显式指定格式字符串或要传递给<a class="reference internal" href="generated/pandas.to_datetime.html#pandas.to_datetime" title="pandas.to_datetime"><code class="xref py py-func docutils literal"><span class="pre">pandas.to_datetime()</span></code></a>的参数的dict：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">pd</span><span class="o">.</span><span class="n">read_sql_table</span><span class="p">(</span><span class="s1">&apos;data&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;Date&apos;</span><span class="p">:</span> <span class="s1">&apos;%Y-%m-</span><span class="si">%d</span><span class="s1">&apos;</span><span class="p">})</span>
<span class="n">pd</span><span class="o">.</span><span class="n">read_sql_table</span><span class="p">(</span><span class="s1">&apos;data&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;Date&apos;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&apos;format&apos;</span><span class="p">:</span> <span class="s1">&apos;%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S&apos;</span><span class="p">}})</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1502">您可以使用<code class="xref py py-func docutils literal"><span class="pre">has_table()</span></code>检查表是否存在</span></p>
</div>
<div class="section" id="schema-support">
<h3><span class="yiyi-st" id="yiyi-1503">Schema support</span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1504"><span class="versionmodified">版本0.15.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1505">通过<a class="reference internal" href="generated/pandas.read_sql_table.html#pandas.read_sql_table" title="pandas.read_sql_table"><code class="xref py py-func docutils literal"><span class="pre">read_sql_table()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.to_sql.html#pandas.DataFrame.to_sql" title="pandas.DataFrame.to_sql"><code class="xref py py-func docutils literal"><span class="pre">to_sql()</span></code></a>函数中的<code class="docutils literal"><span class="pre">schema</span></code>关键字支持读取和写入不同模式。</span><span class="yiyi-st" id="yiyi-1506">注意，这取决于数据库风格（sqlite没有模式）。</span><span class="yiyi-st" id="yiyi-1507">例如：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">to_sql</span><span class="p">(</span><span class="s1">&apos;table&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="s1">&apos;other_schema&apos;</span><span class="p">)</span>
<span class="n">pd</span><span class="o">.</span><span class="n">read_sql_table</span><span class="p">(</span><span class="s1">&apos;table&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">,</span> <span class="n">schema</span><span class="o">=</span><span class="s1">&apos;other_schema&apos;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="id3">
<h3><span class="yiyi-st" id="yiyi-1508">Querying</span></h3>
<p><span class="yiyi-st" id="yiyi-1509">您可以在<a class="reference internal" href="generated/pandas.read_sql_query.html#pandas.read_sql_query" title="pandas.read_sql_query"><code class="xref py py-func docutils literal"><span class="pre">read_sql_query()</span></code></a>函数中使用原始SQL进行查询。</span><span class="yiyi-st" id="yiyi-1510">在这种情况下，必须使用适合于数据库的SQL变量。</span><span class="yiyi-st" id="yiyi-1511">当使用SQLAlchemy时，还可以传递SQLAlchemy Expression语言构造，这是数据库不可知的。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [474]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_sql_query</span><span class="p">(</span><span class="s1">&apos;SELECT * FROM data&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">)</span>
<span class="gr">Out[474]: </span>
<span class="go">   index  id                        Date Col_1  Col_2  Col_3</span>
<span class="go">0      0  26  2010-10-18 00:00:00.000000     X  27.50      1</span>
<span class="go">1      1  42  2010-10-19 00:00:00.000000     Y -12.50      0</span>
<span class="go">2      2  63  2010-10-20 00:00:00.000000     Z   5.73      1</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1512">当然，你可以指定一个更“复杂”的查询。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [475]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_sql_query</span><span class="p">(</span><span class="s2">&quot;SELECT id, Col_1, Col_2 FROM data WHERE id = 42;&quot;</span><span class="p">,</span> <span class="n">engine</span><span class="p">)</span>
<span class="gr">Out[475]: </span>
<span class="go">   id Col_1  Col_2</span>
<span class="go">0  42     Y  -12.5</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1513"><a class="reference internal" href="generated/pandas.read_sql_query.html#pandas.read_sql_query" title="pandas.read_sql_query"><code class="xref py py-func docutils literal"><span class="pre">read_sql_query()</span></code></a>函数支持<code class="docutils literal"><span class="pre">chunksize</span></code>参数。</span><span class="yiyi-st" id="yiyi-1514">指定这将返回一个迭代器通过查询结果的块：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [476]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;abc&apos;</span><span class="p">))</span>

<span class="gp">In [477]: </span><span class="n">df</span><span class="o">.</span><span class="n">to_sql</span><span class="p">(</span><span class="s1">&apos;data_chunks&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [478]: </span><span class="k">for</span> <span class="n">chunk</span> <span class="ow">in</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_sql_query</span><span class="p">(</span><span class="s2">&quot;SELECT * FROM data_chunks&quot;</span><span class="p">,</span> <span class="n">engine</span><span class="p">,</span> <span class="n">chunksize</span><span class="o">=</span><span class="mi">5</span><span class="p">):</span>
<span class="gp">   .....:</span>     <span class="k">print</span><span class="p">(</span><span class="n">chunk</span><span class="p">)</span>
<span class="gp">   .....:</span> 
<span class="go">          a         b         c</span>
<span class="go">0  0.280665 -0.073113  1.160339</span>
<span class="go">1  0.369493  1.904659  1.111057</span>
<span class="go">2  0.659050 -1.627438  0.602319</span>
<span class="go">3  0.420282  0.810952  1.044442</span>
<span class="go">4 -0.400878  0.824006 -0.562305</span>
<span class="go">          a         b         c</span>
<span class="go">0  1.954878 -1.331952 -1.760689</span>
<span class="go">1 -1.650721 -0.890556 -1.119115</span>
<span class="go">2  1.956079 -0.326499 -1.342676</span>
<span class="go">3  1.114383 -0.586524 -1.236853</span>
<span class="go">4  0.875839  0.623362 -0.434957</span>
<span class="go">          a         b         c</span>
<span class="go">0  1.407540  0.129102  1.616950</span>
<span class="go">1  0.502741  1.558806  0.109403</span>
<span class="go">2 -1.219744  2.449369 -0.545774</span>
<span class="go">3 -0.198838 -0.700399 -0.203394</span>
<span class="go">4  0.242669  0.201830  0.661020</span>
<span class="go">          a         b         c</span>
<span class="go">0  1.792158 -0.120465 -1.233121</span>
<span class="go">1 -1.182318 -0.665755 -1.674196</span>
<span class="go">2  0.825030 -0.498214 -0.310985</span>
<span class="go">3 -0.001891 -1.396620 -0.861316</span>
<span class="go">4  0.674712  0.618539 -0.443172</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1515">您还可以运行纯查询，而不使用<code class="xref py py-func docutils literal"><span class="pre">execute()</span></code>创建数据帧。</span><span class="yiyi-st" id="yiyi-1516">这对于不返回值的查询很有用，例如INSERT。</span><span class="yiyi-st" id="yiyi-1517">这在功能上等同于在SQLAlchemy引擎或db连接对象上调用<code class="docutils literal"><span class="pre">execute</span></code>。</span><span class="yiyi-st" id="yiyi-1518">同样，您必须使用适合您的数据库的SQL语法变体。</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pandas.io</span> <span class="kn">import</span> <span class="n">sql</span>
<span class="n">sql</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s1">&apos;SELECT * FROM table_name&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">)</span>
<span class="n">sql</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s1">&apos;INSERT INTO table_name VALUES(?, ?, ?)&apos;</span><span class="p">,</span> <span class="n">engine</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="p">[(</span><span class="s1">&apos;id&apos;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mf">12.2</span><span class="p">,</span> <span class="bp">True</span><span class="p">)])</span>
</pre></div>
</div>
</div>
<div class="section" id="engine-connection-examples">
<h3><span class="yiyi-st" id="yiyi-1519">Engine connection examples</span></h3>
<p><span class="yiyi-st" id="yiyi-1520">要使用SQLAlchemy连接，可以使用<code class="xref py py-func docutils literal"><span class="pre">create_engine()</span></code>函数从数据库URI创建引擎对象。</span><span class="yiyi-st" id="yiyi-1521">每个要连接的数据库只需创建引擎一次。</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">create_engine</span>

<span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">&apos;postgresql://scott:tiger@localhost:5432/mydatabase&apos;</span><span class="p">)</span>

<span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">&apos;mysql+mysqldb://scott:tiger@localhost/foo&apos;</span><span class="p">)</span>

<span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">&apos;oracle://scott:tiger@127.0.0.1:1521/sidname&apos;</span><span class="p">)</span>

<span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">&apos;mssql+pyodbc://mydsn&apos;</span><span class="p">)</span>

<span class="c1"># sqlite://&lt;nohostname&gt;/&lt;path&gt;</span>
<span class="c1"># where &lt;path&gt; is relative:</span>
<span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">&apos;sqlite:///foo.db&apos;</span><span class="p">)</span>

<span class="c1"># or absolute, starting with a slash:</span>
<span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">&apos;sqlite:////absolute/path/to/foo.db&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1522">有关更多信息，请参见SQLAlchemy <a class="reference external" href="http://docs.sqlalchemy.org/en/latest/core/engines.html">文档示例</a></span></p>
</div>
<div class="section" id="advanced-sqlalchemy-queries">
<h3><span class="yiyi-st" id="yiyi-1523">Advanced SQLAlchemy queries</span></h3>
<p><span class="yiyi-st" id="yiyi-1524">您可以使用SQLAlchemy结构来描述您的查询。</span></p>
<p><span class="yiyi-st" id="yiyi-1525">使用<code class="xref py py-func docutils literal"><span class="pre">sqlalchemy.text()</span></code>以后端中性方式指定查询参数</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [479]: </span><span class="kn">import</span> <span class="nn">sqlalchemy</span> <span class="kn">as</span> <span class="nn">sa</span>

<span class="gp">In [480]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_sql</span><span class="p">(</span><span class="n">sa</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s1">&apos;SELECT * FROM data where Col_1=:col1&apos;</span><span class="p">),</span> <span class="n">engine</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;col1&apos;</span><span class="p">:</span> <span class="s1">&apos;X&apos;</span><span class="p">})</span>
<span class="gr">Out[480]: </span>
<span class="go">   index  id                        Date Col_1  Col_2  Col_3</span>
<span class="go">0      0  26  2010-10-18 00:00:00.000000     X   27.5      1</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1526">如果您有数据库的SQLAlchemy描述，可以使用SQLAlchemy表达式表达条件</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [481]: </span><span class="n">metadata</span> <span class="o">=</span> <span class="n">sa</span><span class="o">.</span><span class="n">MetaData</span><span class="p">()</span>

<span class="gp">In [482]: </span><span class="n">data_table</span> <span class="o">=</span> <span class="n">sa</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="s1">&apos;data&apos;</span><span class="p">,</span> <span class="n">metadata</span><span class="p">,</span>
<span class="gp">   .....:</span>     <span class="n">sa</span><span class="o">.</span><span class="n">Column</span><span class="p">(</span><span class="s1">&apos;index&apos;</span><span class="p">,</span> <span class="n">sa</span><span class="o">.</span><span class="n">Integer</span><span class="p">),</span>
<span class="gp">   .....:</span>     <span class="n">sa</span><span class="o">.</span><span class="n">Column</span><span class="p">(</span><span class="s1">&apos;Date&apos;</span><span class="p">,</span> <span class="n">sa</span><span class="o">.</span><span class="n">DateTime</span><span class="p">),</span>
<span class="gp">   .....:</span>     <span class="n">sa</span><span class="o">.</span><span class="n">Column</span><span class="p">(</span><span class="s1">&apos;Col_1&apos;</span><span class="p">,</span> <span class="n">sa</span><span class="o">.</span><span class="n">String</span><span class="p">),</span>
<span class="gp">   .....:</span>     <span class="n">sa</span><span class="o">.</span><span class="n">Column</span><span class="p">(</span><span class="s1">&apos;Col_2&apos;</span><span class="p">,</span> <span class="n">sa</span><span class="o">.</span><span class="n">Float</span><span class="p">),</span>
<span class="gp">   .....:</span>     <span class="n">sa</span><span class="o">.</span><span class="n">Column</span><span class="p">(</span><span class="s1">&apos;Col_3&apos;</span><span class="p">,</span> <span class="n">sa</span><span class="o">.</span><span class="n">Boolean</span><span class="p">),</span>
<span class="gp">   .....:</span> <span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [483]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_sql</span><span class="p">(</span><span class="n">sa</span><span class="o">.</span><span class="n">select</span><span class="p">([</span><span class="n">data_table</span><span class="p">])</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">data_table</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">Col_3</span> <span class="o">==</span> <span class="bp">True</span><span class="p">),</span> <span class="n">engine</span><span class="p">)</span>
<span class="gr">Out[483]: </span>
<span class="go">   index       Date Col_1  Col_2 Col_3</span>
<span class="go">0      0 2010-10-18     X  27.50  True</span>
<span class="go">1      2 2010-10-20     Z   5.73  True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1527">您可以使用<code class="xref py py-func docutils literal"><span class="pre">sqlalchemy.bindparam()</span></code>将SQLAlchemy表达式与传递给<a class="reference internal" href="generated/pandas.read_sql.html#pandas.read_sql" title="pandas.read_sql"><code class="xref py py-func docutils literal"><span class="pre">read_sql()</span></code></a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [484]: </span><span class="kn">import</span> <span class="nn">datetime</span> <span class="kn">as</span> <span class="nn">dt</span>

<span class="gp">In [485]: </span><span class="n">expr</span> <span class="o">=</span> <span class="n">sa</span><span class="o">.</span><span class="n">select</span><span class="p">([</span><span class="n">data_table</span><span class="p">])</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">data_table</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">Date</span> <span class="o">&gt;</span> <span class="n">sa</span><span class="o">.</span><span class="n">bindparam</span><span class="p">(</span><span class="s1">&apos;date&apos;</span><span class="p">))</span>

<span class="gp">In [486]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_sql</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">engine</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;date&apos;</span><span class="p">:</span> <span class="n">dt</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2010</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">18</span><span class="p">)})</span>
<span class="gr">Out[486]: </span>
<span class="go">   index       Date Col_1  Col_2  Col_3</span>
<span class="go">0      1 2010-10-19     Y -12.50  False</span>
<span class="go">1      2 2010-10-20     Z   5.73   True</span>
</pre></div>
</div>
</div>
<div class="section" id="sqlite-fallback">
<h3><span class="yiyi-st" id="yiyi-1528">Sqlite fallback</span></h3>
<p><span class="yiyi-st" id="yiyi-1529">不使用SQLAlchemy支持sqlite的使用。</span><span class="yiyi-st" id="yiyi-1530">此模式需要一个尊重<a class="reference external" href="http://www.python.org/dev/peps/pep-0249/">Python DB-API</a>的Python数据库适配器。</span></p>
<p><span class="yiyi-st" id="yiyi-1531">您可以创建如下所示的连接：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sqlite3</span>
<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&apos;:memory:&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1532">然后发出以下查询：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">data</span><span class="o">.</span><span class="n">to_sql</span><span class="p">(</span><span class="s1">&apos;data&apos;</span><span class="p">,</span> <span class="n">cnx</span><span class="p">)</span>
<span class="n">pd</span><span class="o">.</span><span class="n">read_sql_query</span><span class="p">(</span><span class="s2">&quot;SELECT * FROM data&quot;</span><span class="p">,</span> <span class="n">con</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="google-bigquery-experimental">
<span id="io-bigquery"></span><h2><span class="yiyi-st" id="yiyi-1533">Google BigQuery (Experimental)</span></h2>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1534"><span class="versionmodified">版本0.13.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1535"><code class="xref py py-mod docutils literal"><span class="pre">pandas.io.gbq</span></code>模块为Google的BigQuery分析网络服务提供了一个包装器，以简化使用类似SQL的查询从BigQuery表中检索结果。</span><span class="yiyi-st" id="yiyi-1536">结果集将解析为具有从源表派生的形状和数据类型的pandas DataFrame。</span><span class="yiyi-st" id="yiyi-1537">此外，DataFrames可以插入新的BigQuery表或附加到现有表。</span></p>
<p><span class="yiyi-st" id="yiyi-1538">您将需要安装一些额外的依赖：</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-1539">Google的<a class="reference external" href="https://github.com/google/python-gflags/">python-gflags</a></span></li>
<li><span class="yiyi-st" id="yiyi-1540"><a class="reference external" href="http://pypi.python.org/pypi/httplib2">httplib2</a></span></li>
<li><span class="yiyi-st" id="yiyi-1541"><a class="reference external" href="http://github.com/google/google-api-python-client">google-api-python-client</a></span></li>
</ul>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1542">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1543">要使用此模块，您需要一个有效的BigQuery帐户。</span><span class="yiyi-st" id="yiyi-1544">有关服务本身的详细信息，请参阅<a class="reference external" href="https://cloud.google.com/bigquery/what-is-bigquery">BigQuery文档</a>。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1545">主要功能有：</span></p>
<table border="1" class="longtable docutils">
<colgroup>
<col width="10%">
<col width="90%">
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-1546"><a class="reference internal" href="generated/pandas.io.gbq.read_gbq.html#pandas.io.gbq.read_gbq" title="pandas.io.gbq.read_gbq"><code class="xref py py-obj docutils literal"><span class="pre">read_gbq</span></code></a>（query [，project_id，index_col，...]）</span></td>
<td><span class="yiyi-st" id="yiyi-1547">从Google BigQuery载入数据。</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-1548"><a class="reference internal" href="generated/pandas.io.gbq.to_gbq.html#pandas.io.gbq.to_gbq" title="pandas.io.gbq.to_gbq"><code class="xref py py-obj docutils literal"><span class="pre">to_gbq</span></code></a>（dataframe，destination_table，project_id）</span></td>
<td><span class="yiyi-st" id="yiyi-1549">将DataFrame写入Google BigQuery表格。</span></td>
</tr>
</tbody>
</table>
<div class="section" id="authentication">
<span id="io-bigquery-authentication"></span><span id="io-bigquery-reader"></span><h3><span class="yiyi-st" id="yiyi-1550">Authentication</span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1551"><span class="versionmodified">版本0.18.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1552">Google <code class="docutils literal"><span class="pre">BigQuery</span></code>服务的验证是通过<code class="docutils literal"><span class="pre">OAuth</span> <span class="pre">2.0</span></code>进行。</span><span class="yiyi-st" id="yiyi-1553">可以使用用户帐户凭据或服务帐户凭据进行身份验证。</span></p>
<p><span class="yiyi-st" id="yiyi-1554">使用用户帐户凭据进行身份验证与浏览器窗口中的提示一样简单，会自动为您打开。</span><span class="yiyi-st" id="yiyi-1555">您将使用产品名称<code class="docutils literal"><span class="pre">pandas</span> <span class="pre">GBQ</span></code>向指定的<code class="docutils literal"><span class="pre">BigQuery</span></code>帐户验证身份。</span><span class="yiyi-st" id="yiyi-1556">它只能在本地主机上。</span><span class="yiyi-st" id="yiyi-1557">Pandas当前不支持使用用户帐户凭据的远程身份验证。</span><span class="yiyi-st" id="yiyi-1558">有关身份验证机制的其他信息，请参见<a class="reference external" href="https://developers.google.com/identity/protocols/OAuth2#clientside/">此处</a>。</span></p>
<p><span class="yiyi-st" id="yiyi-1559">可以通过<cite>&apos;private_key&apos;</cite>参数进行具有服务帐户凭据的身份验证。</span><span class="yiyi-st" id="yiyi-1560">当在远程服务器上工作时，此方法特别有用（例如。</span><span class="yiyi-st" id="yiyi-1561">远程主机上的jupyter iPython笔记本）。</span><span class="yiyi-st" id="yiyi-1562">有关服务帐户的其他信息，请访问<a class="reference external" href="https://developers.google.com/identity/protocols/OAuth2#serviceaccount">此处</a>。</span></p>
<p><span class="yiyi-st" id="yiyi-1563">您需要安装附加的依赖项：<a class="reference external" href="https://github.com/google/oauth2client">oauth2client</a>。</span></p>
<p><span class="yiyi-st" id="yiyi-1564">还可以通过<code class="docutils literal"><span class="pre">应用</span> <span class="pre">默认</span> <span class="pre">凭据</span></code></span><span class="yiyi-st" id="yiyi-1565">这仅在未提供参数<code class="docutils literal"><span class="pre">private_key</span></code>时有效。</span><span class="yiyi-st" id="yiyi-1566">此方法还要求可以从代码运行的环境中获取凭据。</span><span class="yiyi-st" id="yiyi-1567">否则，将使用OAuth2客户端身份验证。</span><span class="yiyi-st" id="yiyi-1568">有关<a class="reference external" href="https://developers.google.com/identity/protocols/application-default-credentials">应用程序默认凭据</a>的其他信息。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1569"><span class="versionmodified">版本0.19.0中的新功能。</span></span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1570">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1571"><cite>&apos;private_key&apos;</cite>参数可以设置为JSON格式的服务帐户密钥的文件路径，或JSON格式的服务帐户密钥的密钥内容。</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1572">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1573">您可以点击<a class="reference external" href="https://console.developers.google.com/permissions/serviceaccounts">此处</a>从Google开发者控制台获取私钥。</span><span class="yiyi-st" id="yiyi-1574">使用JSON键类型。</span></p>
</div>
</div>
<div class="section" id="id4">
<h3><span class="yiyi-st" id="yiyi-1575">Querying</span></h3>
<p><span class="yiyi-st" id="yiyi-1576">假设您要使用<a class="reference internal" href="generated/pandas.io.gbq.read_gbq.html#pandas.io.gbq.read_gbq" title="pandas.io.gbq.read_gbq"><code class="xref py py-func docutils literal"><span class="pre">read_gbq()</span></code></a>函数将现有BigQuery表中的所有数据加载到DataFrame：<cite>test_dataset.test_table</cite></span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># Insert your BigQuery Project ID Here</span>
<span class="c1"># Can be found in the Google web console</span>
<span class="n">projectid</span> <span class="o">=</span> <span class="s2">&quot;xxxxxxxx&quot;</span>

<span class="n">data_frame</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_gbq</span><span class="p">(</span><span class="s1">&apos;SELECT * FROM test_dataset.test_table&apos;</span><span class="p">,</span> <span class="n">projectid</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1577">您可以定义BigQuery中的哪个列用作目标DataFrame中的索引以及首选列顺序，如下所示：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">data_frame</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_gbq</span><span class="p">(</span><span class="s1">&apos;SELECT * FROM test_dataset.test_table&apos;</span><span class="p">,</span>
                          <span class="n">index_col</span><span class="o">=</span><span class="s1">&apos;index_column_name&apos;</span><span class="p">,</span>
                          <span class="n">col_order</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;col1&apos;</span><span class="p">,</span> <span class="s1">&apos;col2&apos;</span><span class="p">,</span> <span class="s1">&apos;col3&apos;</span><span class="p">],</span> <span class="n">projectid</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1578">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1579">您可以在<a class="reference external" href="https://console.developers.google.com">Google开发人员控制台</a>中找到您的专案ID。</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1580">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1581">您可以通过默认为<code class="docutils literal"><span class="pre">True</span></code>的<code class="docutils literal"><span class="pre">verbose</span></code>标志切换详细输出。</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1582">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1583"><code class="docutils literal"><span class="pre">dialect</span></code>参数可用于指示是否使用BigQuery的<code class="docutils literal"><span class="pre">&apos;legacy&apos;</span></code> SQL或BigQuery的<code class="docutils literal"><span class="pre">&apos;standard&apos;</span></code> SQL（测试版）。</span><span class="yiyi-st" id="yiyi-1584">默认值为<code class="docutils literal"><span class="pre">&apos;legacy&apos;</span></code>。</span><span class="yiyi-st" id="yiyi-1585">有关BigQuery标准SQL的详情，请参阅<a class="reference external" href="https://cloud.google.com/bigquery/sql-reference/">BigQuery SQL参考</a></span></p>
</div>
</div>
<div class="section" id="io-bigquery-writer">
<span id="id5"></span><h3><span class="yiyi-st" id="yiyi-1586">Writing DataFrames</span></h3>
<p><span class="yiyi-st" id="yiyi-1587">假设我们要使用<a class="reference internal" href="generated/pandas.DataFrame.to_gbq.html#pandas.DataFrame.to_gbq" title="pandas.DataFrame.to_gbq"><code class="xref py py-func docutils literal"><span class="pre">to_gbq()</span></code></a>将DataFrame <code class="docutils literal"><span class="pre">df</span></code>写入BigQuery表格中。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [487]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;my_string&apos;</span><span class="p">:</span> <span class="nb">list</span><span class="p">(</span><span class="s1">&apos;abc&apos;</span><span class="p">),</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;my_int64&apos;</span><span class="p">:</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">)),</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;my_float64&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">4.0</span><span class="p">,</span> <span class="mf">7.0</span><span class="p">),</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;my_bool1&apos;</span><span class="p">:</span> <span class="p">[</span><span class="bp">True</span><span class="p">,</span> <span class="bp">False</span><span class="p">,</span> <span class="bp">True</span><span class="p">],</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;my_bool2&apos;</span><span class="p">:</span> <span class="p">[</span><span class="bp">False</span><span class="p">,</span> <span class="bp">True</span><span class="p">,</span> <span class="bp">False</span><span class="p">],</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;my_dates&apos;</span><span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;now&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">3</span><span class="p">)})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [488]: </span><span class="n">df</span>
<span class="gr">Out[488]: </span>
<span class="go">  my_bool1 my_bool2                   my_dates  my_float64  my_int64 my_string</span>
<span class="go">0     True    False 2016-12-24 18:33:33.411047         4.0         1         a</span>
<span class="go">1    False     True 2016-12-25 18:33:33.411047         5.0         2         b</span>
<span class="go">2     True    False 2016-12-26 18:33:33.411047         6.0         3         c</span>

<span class="gp">In [489]: </span><span class="n">df</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[489]: </span>
<span class="go">my_bool1                bool</span>
<span class="go">my_bool2                bool</span>
<span class="go">my_dates      datetime64[ns]</span>
<span class="go">my_float64           float64</span>
<span class="go">my_int64               int64</span>
<span class="go">my_string             object</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">to_gbq</span><span class="p">(</span><span class="s1">&apos;my_dataset.my_table&apos;</span><span class="p">,</span> <span class="n">projectid</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1588">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1589">如果目标表和目标数据集不存在，将自动创建它们。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1590"><code class="docutils literal"><span class="pre">if_exists</span></code>参数可用于指示是否<code class="docutils literal"><span class="pre">&apos;fail&apos;</span></code>，<code class="docutils literal"><span class="pre">&apos;replace&apos;</span></code>或<code class="docutils literal"><span class="pre">&apos;append&apos;</span></code> if目标表已存在。</span><span class="yiyi-st" id="yiyi-1591">默认值为<code class="docutils literal"><span class="pre">&apos;fail&apos;</span></code>。</span></p>
<p><span class="yiyi-st" id="yiyi-1592">例如，假设<code class="docutils literal"><span class="pre">if_exists</span></code>设置为<code class="docutils literal"><span class="pre">&apos;fail&apos;</span></code>。</span><span class="yiyi-st" id="yiyi-1593">如果目标表已存在，以下代码段将产生<code class="docutils literal"><span class="pre">TableCreationError</span></code>。</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">to_gbq</span><span class="p">(</span><span class="s1">&apos;my_dataset.my_table&apos;</span><span class="p">,</span> <span class="n">projectid</span><span class="p">,</span> <span class="n">if_exists</span><span class="o">=</span><span class="s1">&apos;fail&apos;</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1594">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1595">如果<code class="docutils literal"><span class="pre">if_exists</span></code>参数设置为<code class="docutils literal"><span class="pre">&apos;append&apos;</span></code>，则将使用定义的表模式和列类型将目标数据帧写入表。</span><span class="yiyi-st" id="yiyi-1596">数据帧必须与结构和数据类型中的目标表相匹配。</span><span class="yiyi-st" id="yiyi-1597">如果<code class="docutils literal"><span class="pre">if_exists</span></code>参数设置为<code class="docutils literal"><span class="pre">&apos;replace&apos;</span></code>，并且现有表具有不同的模式，则将强制延迟2分钟以确保新模式已传播在Google环境中。</span><span class="yiyi-st" id="yiyi-1598">请参阅<a class="reference external" href="https://code.google.com/p/google-bigquery/issues/detail?id=191">Google BigQuery问题191</a>。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1599">写入大型DataFrames可能会由于超出大小限制而导致错误。</span><span class="yiyi-st" id="yiyi-1600">这可以通过在调用<a class="reference internal" href="generated/pandas.DataFrame.to_gbq.html#pandas.DataFrame.to_gbq" title="pandas.DataFrame.to_gbq"><code class="xref py py-func docutils literal"><span class="pre">to_gbq()</span></code></a>时设置<code class="docutils literal"><span class="pre">chunksize</span></code>参数来避免。</span><span class="yiyi-st" id="yiyi-1601">例如，以下将<code class="docutils literal"><span class="pre">df</span></code>以批处理形式批量写入BigQuery表中：每次10000行：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">to_gbq</span><span class="p">(</span><span class="s1">&apos;my_dataset.my_table&apos;</span><span class="p">,</span> <span class="n">projectid</span><span class="p">,</span> <span class="n">chunksize</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1602">您还可以通过默认为<code class="docutils literal"><span class="pre">True</span></code>的<code class="docutils literal"><span class="pre">verbose</span></code>标志查看您的帖子的进度。</span><span class="yiyi-st" id="yiyi-1603">例如：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mi">8</span><span class="p">]:</span> <span class="n">df</span><span class="o">.</span><span class="n">to_gbq</span><span class="p">(</span><span class="s1">&apos;my_dataset.my_table&apos;</span><span class="p">,</span> <span class="n">projectid</span><span class="p">,</span> <span class="n">chunksize</span><span class="o">=</span><span class="mi">10000</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

        <span class="n">Streaming</span> <span class="n">Insert</span> <span class="ow">is</span> <span class="mi">10</span><span class="o">%</span> <span class="n">Complete</span>
        <span class="n">Streaming</span> <span class="n">Insert</span> <span class="ow">is</span> <span class="mi">20</span><span class="o">%</span> <span class="n">Complete</span>
        <span class="n">Streaming</span> <span class="n">Insert</span> <span class="ow">is</span> <span class="mi">30</span><span class="o">%</span> <span class="n">Complete</span>
        <span class="n">Streaming</span> <span class="n">Insert</span> <span class="ow">is</span> <span class="mi">40</span><span class="o">%</span> <span class="n">Complete</span>
        <span class="n">Streaming</span> <span class="n">Insert</span> <span class="ow">is</span> <span class="mi">50</span><span class="o">%</span> <span class="n">Complete</span>
        <span class="n">Streaming</span> <span class="n">Insert</span> <span class="ow">is</span> <span class="mi">60</span><span class="o">%</span> <span class="n">Complete</span>
        <span class="n">Streaming</span> <span class="n">Insert</span> <span class="ow">is</span> <span class="mi">70</span><span class="o">%</span> <span class="n">Complete</span>
        <span class="n">Streaming</span> <span class="n">Insert</span> <span class="ow">is</span> <span class="mi">80</span><span class="o">%</span> <span class="n">Complete</span>
        <span class="n">Streaming</span> <span class="n">Insert</span> <span class="ow">is</span> <span class="mi">90</span><span class="o">%</span> <span class="n">Complete</span>
        <span class="n">Streaming</span> <span class="n">Insert</span> <span class="ow">is</span> <span class="mi">100</span><span class="o">%</span> <span class="n">Complete</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1604">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1605">如果在将数据流式传输到BigQuery时发生错误，请参阅<a class="reference external" href="https://cloud.google.com/bigquery/troubleshooting-errors">排查BigQuery错误</a>。</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1606">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1607">BigQuery SQL查询语言有一些异常，请参阅<a class="reference external" href="https://cloud.google.com/bigquery/query-reference">BigQuery查询参考文档</a>。</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1608">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1609">虽然BigQuery使用类似SQL的语法，但它在功能，API限制（查询或上传的大小和数量）以及Google对服务使用收费方面与传统数据库有一些重要差异。</span><span class="yiyi-st" id="yiyi-1610">由于服务似乎在变化和发展，因此您应经常参阅<a class="reference external" href="https://cloud.google.com/bigquery/what-is-bigquery">Google BigQuery文档</a>。</span><span class="yiyi-st" id="yiyi-1611">BiqQuery最适合快速分析大量数据，但不能直接替换事务数据库。</span></p>
</div>
</div>
<div class="section" id="creating-bigquery-tables">
<h3><span class="yiyi-st" id="yiyi-1612">Creating BigQuery Tables</span></h3>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1613">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1614">从0.17开始，函数<code class="xref py py-func docutils literal"><span class="pre">generate_bq_schema()</span></code>已被弃用，并将在未来版本中删除。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1615">从0.15.2开始，gbq模块具有函数<code class="xref py py-func docutils literal"><span class="pre">generate_bq_schema()</span></code>，它将产生指定的pandas DataFrame的字典表示模式。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [10]: </span><span class="n">gbq</span><span class="o">.</span><span class="n">generate_bq_schema</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">default_type</span><span class="o">=</span><span class="s1">&apos;STRING&apos;</span><span class="p">)</span>

<span class="gr">Out[10]: </span><span class="p">{</span><span class="s1">&apos;fields&apos;</span><span class="p">:</span> <span class="p">[{</span><span class="s1">&apos;name&apos;</span><span class="p">:</span> <span class="s1">&apos;my_bool1&apos;</span><span class="p">,</span> <span class="s1">&apos;type&apos;</span><span class="p">:</span> <span class="s1">&apos;BOOLEAN&apos;</span><span class="p">},</span>
<span class="go">         {&apos;name&apos;: &apos;my_bool2&apos;, &apos;type&apos;: &apos;BOOLEAN&apos;},</span>
<span class="go">         {&apos;name&apos;: &apos;my_dates&apos;, &apos;type&apos;: &apos;TIMESTAMP&apos;},</span>
<span class="go">         {&apos;name&apos;: &apos;my_float64&apos;, &apos;type&apos;: &apos;FLOAT&apos;},</span>
<span class="go">         {&apos;name&apos;: &apos;my_int64&apos;, &apos;type&apos;: &apos;INTEGER&apos;},</span>
<span class="go">         {&apos;name&apos;: &apos;my_string&apos;, &apos;type&apos;: &apos;STRING&apos;}]}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1616">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1617">如果您删除并重新创建具有相同名称但表格架构不同的BigQuery表，则必须等待2分钟才能将数据流式传输到表中。</span><span class="yiyi-st" id="yiyi-1618">作为解决方法，请考虑使用不同的名称创建新表。</span><span class="yiyi-st" id="yiyi-1619">请参阅<a class="reference external" href="https://code.google.com/p/google-bigquery/issues/detail?id=191">Google BigQuery问题191</a>。</span></p>
</div>
</div>
</div>
<div class="section" id="stata-format">
<span id="io-stata"></span><h2><span class="yiyi-st" id="yiyi-1620">Stata Format</span></h2>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1621"><span class="versionmodified">版本0.12.0中的新功能。</span></span></p>
</div>
<div class="section" id="writing-to-stata-format">
<span id="io-stata-writer"></span><h3><span class="yiyi-st" id="yiyi-1622">Writing to Stata format</span></h3>
<p><span class="yiyi-st" id="yiyi-1623">方法<code class="xref py py-func docutils literal"><span class="pre">to_stata()</span></code>会将DataFrame写入.dta文件。</span><span class="yiyi-st" id="yiyi-1624">此文件的格式版本始终为115（Stata 12）。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [490]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;AB&apos;</span><span class="p">))</span>

<span class="gp">In [491]: </span><span class="n">df</span><span class="o">.</span><span class="n">to_stata</span><span class="p">(</span><span class="s1">&apos;stata.dta&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1625"><em>Stata</em> data files have limited data type support; only strings with 244 or fewer characters, <code class="docutils literal"><span class="pre">int8</span></code>, <code class="docutils literal"><span class="pre">int16</span></code>, <code class="docutils literal"><span class="pre">int32</span></code>, <code class="docutils literal"><span class="pre">float32</span></code> and <code class="docutils literal"><span class="pre">float64</span></code> can be stored in <code class="docutils literal"><span class="pre">.dta</span></code> files. </span><span class="yiyi-st" id="yiyi-1626">此外，<em>Stata</em>保留某些值以表示缺失的数据。</span><span class="yiyi-st" id="yiyi-1627">导出特定数据类型的Stata中允许范围之外的非缺失值将会将该变量重新键入下一个较大的大小。</span><span class="yiyi-st" id="yiyi-1628">例如，在Stata中，<code class="docutils literal"><span class="pre">int8</span></code>的值被限制在-127和100之间，因此值大于100的变量将触发到<code class="docutils literal"><span class="pre">int16</span></code>的转换。</span><span class="yiyi-st" id="yiyi-1629">浮点数据类型中的<code class="docutils literal"><span class="pre">nan</span></code>值被存储为基本丢失数据类型（<code class="docutils literal"><span class="pre">.</span></code></span><span class="yiyi-st" id="yiyi-1630">在<em>Stata</em>）。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1631">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1632">不能导出整数数据类型的缺失数据值。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1633">The <em>Stata</em> writer gracefully handles other data types including <code class="docutils literal"><span class="pre">int64</span></code>, <code class="docutils literal"><span class="pre">bool</span></code>, <code class="docutils literal"><span class="pre">uint8</span></code>, <code class="docutils literal"><span class="pre">uint16</span></code>, <code class="docutils literal"><span class="pre">uint32</span></code> by casting to the smallest supported type that can represent the data. </span><span class="yiyi-st" id="yiyi-1634">For example, data with a type of <code class="docutils literal"><span class="pre">uint8</span></code> will be cast to <code class="docutils literal"><span class="pre">int8</span></code> if all values are less than 100 (the upper bound for non-missing <code class="docutils literal"><span class="pre">int8</span></code> data in <em>Stata</em>), or, if values are outside of this range, the variable is cast to <code class="docutils literal"><span class="pre">int16</span></code>.</span></p>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1635">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1636">如果<code class="docutils literal"><span class="pre">int64</span></code>值大于2 ** 53，则从<code class="docutils literal"><span class="pre">int64</span></code>到<code class="docutils literal"><span class="pre">float64</span></code>的转换可能会导致精度损失。</span></p>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1637">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1638"><code class="xref py py-class docutils literal"><span class="pre">StataWriter</span></code>和<code class="xref py py-func docutils literal"><span class="pre">to_stata()</span></code>仅支持包含最多244个字符的固定宽度字符串，这是版本115 dta文件格式强加的限制。</span><span class="yiyi-st" id="yiyi-1639">尝试使用长度超过244个字符的字符串写入<em>Stata</em> dta文件会引发<code class="docutils literal"><span class="pre">ValueError</span></code>。</span></p>
</div>
</div>
<div class="section" id="reading-from-stata-format">
<span id="io-stata-reader"></span><h3><span class="yiyi-st" id="yiyi-1640">Reading from Stata format</span></h3>
<p><span class="yiyi-st" id="yiyi-1641">顶层函数<code class="docutils literal"><span class="pre">read_stata</span></code>将读取一个dta文件，并返回一个DataFrame或一个<code class="xref py py-class docutils literal"><span class="pre">StataReader</span></code>，用于以增量方式读取文件。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [492]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_stata</span><span class="p">(</span><span class="s1">&apos;stata.dta&apos;</span><span class="p">)</span>
<span class="gr">Out[492]: </span>
<span class="go">   index         A         B</span>
<span class="go">0      0  1.810535 -1.305727</span>
<span class="go">1      1 -0.344987 -0.230840</span>
<span class="go">2      2 -2.793085  1.937529</span>
<span class="go">3      3  0.366332 -1.044589</span>
<span class="go">4      4  2.051173  0.585662</span>
<span class="go">5      5  0.429526 -0.606998</span>
<span class="go">6      6  0.106223 -1.525680</span>
<span class="go">7      7  0.795026 -0.374438</span>
<span class="go">8      8  0.134048  1.202055</span>
<span class="go">9      9  0.284748  0.262467</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1642"><span class="versionmodified">版本0.16.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1643">指定<code class="docutils literal"><span class="pre">chunksize</span></code>会生成一个<code class="xref py py-class docutils literal"><span class="pre">StataReader</span></code>实例，用于一次从文件中读取<code class="docutils literal"><span class="pre">chunksize</span></code>行。</span><span class="yiyi-st" id="yiyi-1644"><code class="docutils literal"><span class="pre">StataReader</span></code>对象可以用作迭代器。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [493]: </span><span class="n">reader</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_stata</span><span class="p">(</span><span class="s1">&apos;stata.dta&apos;</span><span class="p">,</span> <span class="n">chunksize</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>

<span class="gp">In [494]: </span><span class="k">for</span> <span class="n">df</span> <span class="ow">in</span> <span class="n">reader</span><span class="p">:</span>
<span class="gp">   .....:</span>     <span class="k">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="gp">   .....:</span> 
<span class="go">(3, 3)</span>
<span class="go">(3, 3)</span>
<span class="go">(3, 3)</span>
<span class="go">(1, 3)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1645">对于更细粒度的控制，使用<code class="docutils literal"><span class="pre">iterator=True</span></code>并在每次调用<code class="xref py py-func docutils literal"><span class="pre">read()</span></code>时指定<code class="docutils literal"><span class="pre">chunksize</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [495]: </span><span class="n">reader</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_stata</span><span class="p">(</span><span class="s1">&apos;stata.dta&apos;</span><span class="p">,</span> <span class="n">iterator</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [496]: </span><span class="n">chunk1</span> <span class="o">=</span> <span class="n">reader</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>

<span class="gp">In [497]: </span><span class="n">chunk2</span> <span class="o">=</span> <span class="n">reader</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1646">目前，<code class="docutils literal"><span class="pre">index</span></code>作为列检索。</span></p>
<p><span class="yiyi-st" id="yiyi-1647">参数<code class="docutils literal"><span class="pre">convert_categoricals</span></code>指示是否应读取值标签，并使用它们从中创建<code class="docutils literal"><span class="pre">Categorical</span></code>变量。</span><span class="yiyi-st" id="yiyi-1648">值标签也可以通过函数<code class="docutils literal"><span class="pre">value_labels</span></code>检索，这需要在使用之前调用<code class="xref py py-func docutils literal"><span class="pre">read()</span></code>。</span></p>
<p><span class="yiyi-st" id="yiyi-1649">参数<code class="docutils literal"><span class="pre">convert_missing</span></code>指示是否应保留Stata中的缺失值表示。</span><span class="yiyi-st" id="yiyi-1650">如果<code class="docutils literal"><span class="pre">False</span></code>（默认值），则缺失值表示为<code class="docutils literal"><span class="pre">np.nan</span></code>。</span><span class="yiyi-st" id="yiyi-1651">如果<code class="docutils literal"><span class="pre">True</span></code>，则使用<code class="docutils literal"><span class="pre">StataMissingValue</span></code>对象表示缺失值，包含缺少值的列将具有<code class="docutils literal"><span class="pre">object</span></code>数据类型。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1652">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1653"><a class="reference internal" href="generated/pandas.read_stata.html#pandas.read_stata" title="pandas.read_stata"><code class="xref py py-func docutils literal"><span class="pre">read_stata()</span></code></a>和<code class="xref py py-class docutils literal"><span class="pre">StataReader</span></code>支持.dta格式113-115（Stata 10-12），117（Stata 13）和118（Stata 14）。</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1654">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1655">设置<code class="docutils literal"><span class="pre">preserve_dtypes=False</span></code>将向上转换为标准的pandas数据类型：对于所有整数类型为<code class="docutils literal"><span class="pre">int64</span></code>，对于浮点数据为<code class="docutils literal"><span class="pre">float64</span></code>。</span><span class="yiyi-st" id="yiyi-1656">默认情况下，Stata数据类型在导入时保留。</span></p>
</div>
<div class="section" id="io-stata-categorical">
<span id="id6"></span><h4><span class="yiyi-st" id="yiyi-1657">Categorical Data</span></h4>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1658"><span class="versionmodified">版本0.15.2中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1659"><code class="docutils literal"><span class="pre">Categorical</span></code>数据可以作为值标记数据导出到<em>Stata</em>数据文件。</span><span class="yiyi-st" id="yiyi-1660">导出的数据由作为整数数据值的底层类别代码和作为值标签的类别组成。</span><span class="yiyi-st" id="yiyi-1661"><em>Stata</em>没有显式等效于<code class="docutils literal"><span class="pre">Categorical</span></code>，并且有关<em>变量是否有序的信息在导出时会丢失。</em></span></p>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1662">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1663"><em>Stata</em>仅支持字符串值标签，因此导出数据时，类别上调用<code class="docutils literal"><span class="pre">str</span></code>。</span><span class="yiyi-st" id="yiyi-1664">导出具有非字符串类别的<code class="docutils literal"><span class="pre">Categorical</span></code>变量​​会产生警告，如果类别的<code class="docutils literal"><span class="pre">str</span></code>表示不唯一，则可能导致信息丢失。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1665">使用关键字参数<code class="docutils literal"><span class="pre">convert_categoricals</span></code>（默认情况下为<code class="docutils literal"><span class="pre">True</span></code>），可以类似地将<em>Stata</em>数据文件中的标签数据导入为<code class="docutils literal"><span class="pre">Categorical</span></code></span><span class="yiyi-st" id="yiyi-1666">关键字参数<code class="docutils literal"><span class="pre">order_categoricals</span></code>（默认情况下为<code class="docutils literal"><span class="pre">True</span></code>）决定导入的<code class="docutils literal"><span class="pre">Categorical</span></code>变量是否有序。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1667">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1668">导入分类数据时，<em>Stata</em>数据文件中的变量值不会保留，因为<code class="docutils literal"><span class="pre">Categorical</span></code>变量始终使用整数数据类型<code class="docutils literal"><span class="pre">-1</span></code>和<code class="docutils literal"><span class="pre">n-1</span></code>其中<code class="docutils literal"><span class="pre">n</span></code>是类别的数量。</span><span class="yiyi-st" id="yiyi-1669">如果需要<em>Stata</em>数据文件中的原始值，可以通过设置<code class="docutils literal"><span class="pre">convert_categoricals=False</span></code>导入这些值，这将导入原始数据（但不导入变量标签）。</span><span class="yiyi-st" id="yiyi-1670">The original values can be matched to the imported categorical data since there is a simple mapping between the original <em>Stata</em> data values and the category codes of imported Categorical variables: missing values are assigned code <code class="docutils literal"><span class="pre">-1</span></code>, and the smallest original value is assigned <code class="docutils literal"><span class="pre">0</span></code>, the second smallest is assigned <code class="docutils literal"><span class="pre">1</span></code> and so on until the largest original value is assigned the code <code class="docutils literal"><span class="pre">n-1</span></code>.</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-1671">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-1672"><em>Stata</em>支持部分标记的系列。</span><span class="yiyi-st" id="yiyi-1673">这些系列具有一些但不是所有数据值的值标签。</span><span class="yiyi-st" id="yiyi-1674">Importing a partially labeled series will produce a <code class="docutils literal"><span class="pre">Categorical</span></code> with string categories for the values that are labeled and numeric categories for values with no label.</span></p>
</div>
</div>
</div>
</div>
<div class="section" id="sas-formats">
<span id="io-sas-reader"></span><span id="io-sas"></span><h2><span class="yiyi-st" id="yiyi-1675">SAS Formats</span></h2>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-1676"><span class="versionmodified">版本0.17.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-1677">顶层函数<a class="reference internal" href="generated/pandas.read_sas.html#pandas.read_sas" title="pandas.read_sas"><code class="xref py py-func docutils literal"><span class="pre">read_sas()</span></code></a>可读取SAS <cite>xport</cite>（.XPT）和<cite>SAS7BDAT</cite>（.sas7bdat）格式文件在<em>v0.18.0</em>中添加。</span></p>
<p><span class="yiyi-st" id="yiyi-1678">SAS文件只包含两种值类型：ASCII文本和浮点值（通常为8字节，但有时被截断）。</span><span class="yiyi-st" id="yiyi-1679">对于xport文件，没有自动类型转换为整数，日期或分类。</span><span class="yiyi-st" id="yiyi-1680">对于SAS7BDAT文件，格式代码可以允许日期变量自动转换为日期。</span><span class="yiyi-st" id="yiyi-1681">默认情况下，读取整个文件并将其作为<code class="docutils literal"><span class="pre">DataFrame</span></code>返回。</span></p>
<p><span class="yiyi-st" id="yiyi-1682">指定<code class="docutils literal"><span class="pre">chunksize</span></code>或使用<code class="docutils literal"><span class="pre">iterator=True</span></code>获取读取器对象（<code class="docutils literal"><span class="pre">XportReader</span></code>或<code class="docutils literal"><span class="pre">SAS7BDATReader</span></code>）， 。</span><span class="yiyi-st" id="yiyi-1683">阅读器对象还具有包含有关文件及其变量的附加信息的属性。</span></p>
<p><span class="yiyi-st" id="yiyi-1684">读取SAS7BDAT文件：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_sas</span><span class="p">(</span><span class="s1">&apos;sas_data.sas7bdat&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1685">获取一个迭代器，并一次读取一个XPORT文件10万行：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">rdr</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_sas</span><span class="p">(</span><span class="s1">&apos;sas_xport.xpt&apos;</span><span class="p">,</span> <span class="n">chunk</span><span class="o">=</span><span class="mi">100000</span><span class="p">)</span>
<span class="k">for</span> <span class="n">chunk</span> <span class="ow">in</span> <span class="n">rdr</span><span class="p">:</span>
    <span class="n">do_something</span><span class="p">(</span><span class="n">chunk</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1686">xport文件格式的<a class="reference external" href="https://support.sas.com/techsup/technote/ts140.pdf">规范</a>可从SAS网站获取。</span></p>
<p><span class="yiyi-st" id="yiyi-1687">没有官方文档可用于SAS7BDAT格式。</span></p>
</div>
<div class="section" id="other-file-formats">
<span id="io-other"></span><h2><span class="yiyi-st" id="yiyi-1688">Other file formats</span></h2>
<p><span class="yiyi-st" id="yiyi-1689">pandas本身只支持有一组有限的文件格式的IO，它们清楚地映射到它的表格数据模型。</span><span class="yiyi-st" id="yiyi-1690">对于从pandas中读取和写入其他文件格式，我们推荐来自更广泛的社区的这些包。</span></p>
<div class="section" id="netcdf">
<h3><span class="yiyi-st" id="yiyi-1691">netCDF</span></h3>
<p><span class="yiyi-st" id="yiyi-1692"><a class="reference external" href="http://xarray.pydata.org/">xarray</a>提供了由pandas DataFrame启发的用于处理多维数据集的数据结构，重点是netCDF文件格式，并且易于转换到pandas。</span></p>
</div>
</div>
<div class="section" id="performance-considerations">
<span id="io-perf"></span><h2><span class="yiyi-st" id="yiyi-1693">Performance Considerations</span></h2>
<p><span class="yiyi-st" id="yiyi-1694">这是使用pandas 0.13.1的各种IO方法的非正式比较。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">1000000</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span><span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;AB&apos;</span><span class="p">))</span>

<span class="gp">In [2]: </span><span class="n">df</span><span class="o">.</span><span class="n">info</span><span class="p">()</span>
<span class="go">&lt;class &apos;pandas.core.frame.DataFrame&apos;&gt;</span>
<span class="go">Int64Index: 1000000 entries, 0 to 999999</span>
<span class="go">Data columns (total 2 columns):</span>
<span class="go">A    1000000 non-null float64</span>
<span class="go">B    1000000 non-null float64</span>
<span class="go">dtypes: float64(2)</span>
<span class="go">memory usage: 22.9 MB</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1695">写作</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [14]: </span><span class="o">%</span><span class="n">timeit</span> <span class="n">test_sql_write</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
<span class="go">1 loops, best of 3: 6.24 s per loop</span>

<span class="gp">In [15]: </span><span class="o">%</span><span class="n">timeit</span> <span class="n">test_hdf_fixed_write</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
<span class="go">1 loops, best of 3: 237 ms per loop</span>

<span class="gp">In [26]: </span><span class="o">%</span><span class="n">timeit</span> <span class="n">test_hdf_fixed_write_compress</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
<span class="go">1 loops, best of 3: 245 ms per loop</span>

<span class="gp">In [16]: </span><span class="o">%</span><span class="n">timeit</span> <span class="n">test_hdf_table_write</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
<span class="go">1 loops, best of 3: 901 ms per loop</span>

<span class="gp">In [27]: </span><span class="o">%</span><span class="n">timeit</span> <span class="n">test_hdf_table_write_compress</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
<span class="go">1 loops, best of 3: 952 ms per loop</span>

<span class="gp">In [17]: </span><span class="o">%</span><span class="n">timeit</span> <span class="n">test_csv_write</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
<span class="go">1 loops, best of 3: 3.44 s per loop</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1696">读</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [18]: </span><span class="o">%</span><span class="n">timeit</span> <span class="n">test_sql_read</span><span class="p">()</span>
<span class="go">1 loops, best of 3: 766 ms per loop</span>

<span class="gp">In [19]: </span><span class="o">%</span><span class="n">timeit</span> <span class="n">test_hdf_fixed_read</span><span class="p">()</span>
<span class="go">10 loops, best of 3: 19.1 ms per loop</span>

<span class="gp">In [28]: </span><span class="o">%</span><span class="n">timeit</span> <span class="n">test_hdf_fixed_read_compress</span><span class="p">()</span>
<span class="go">10 loops, best of 3: 36.3 ms per loop</span>

<span class="gp">In [20]: </span><span class="o">%</span><span class="n">timeit</span> <span class="n">test_hdf_table_read</span><span class="p">()</span>
<span class="go">10 loops, best of 3: 39 ms per loop</span>

<span class="gp">In [29]: </span><span class="o">%</span><span class="n">timeit</span> <span class="n">test_hdf_table_read_compress</span><span class="p">()</span>
<span class="go">10 loops, best of 3: 60.6 ms per loop</span>

<span class="gp">In [22]: </span><span class="o">%</span><span class="n">timeit</span> <span class="n">test_csv_read</span><span class="p">()</span>
<span class="go">1 loops, best of 3: 620 ms per loop</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1697">磁盘空间（以字节为单位）</span></p>
<div class="highlight-none"><div class="highlight"><pre><span></span>25843712 Apr  8 14:11 test.sql
24007368 Apr  8 14:11 test_fixed.hdf
15580682 Apr  8 14:11 test_fixed_compress.hdf
24458444 Apr  8 14:11 test_table.hdf
16797283 Apr  8 14:11 test_table_compress.hdf
46152810 Apr  8 14:11 test.csv
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-1698">这里是代码</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sqlite3</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">pandas.io</span> <span class="kn">import</span> <span class="n">sql</span>

<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">1000000</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span><span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;AB&apos;</span><span class="p">))</span>

<span class="k">def</span> <span class="nf">test_sql_write</span><span class="p">(</span><span class="n">df</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">&apos;test.sql&apos;</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s1">&apos;test.sql&apos;</span><span class="p">)</span>
    <span class="n">sql_db</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&apos;test.sql&apos;</span><span class="p">)</span>
    <span class="n">df</span><span class="o">.</span><span class="n">to_sql</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&apos;test_table&apos;</span><span class="p">,</span> <span class="n">con</span><span class="o">=</span><span class="n">sql_db</span><span class="p">)</span>
    <span class="n">sql_db</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">test_sql_read</span><span class="p">():</span>
    <span class="n">sql_db</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&apos;test.sql&apos;</span><span class="p">)</span>
    <span class="n">pd</span><span class="o">.</span><span class="n">read_sql_query</span><span class="p">(</span><span class="s2">&quot;select * from test_table&quot;</span><span class="p">,</span> <span class="n">sql_db</span><span class="p">)</span>
    <span class="n">sql_db</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">test_hdf_fixed_write</span><span class="p">(</span><span class="n">df</span><span class="p">):</span>
    <span class="n">df</span><span class="o">.</span><span class="n">to_hdf</span><span class="p">(</span><span class="s1">&apos;test_fixed.hdf&apos;</span><span class="p">,</span><span class="s1">&apos;test&apos;</span><span class="p">,</span><span class="n">mode</span><span class="o">=</span><span class="s1">&apos;w&apos;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">test_hdf_fixed_read</span><span class="p">():</span>
    <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="s1">&apos;test_fixed.hdf&apos;</span><span class="p">,</span><span class="s1">&apos;test&apos;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">test_hdf_fixed_write_compress</span><span class="p">(</span><span class="n">df</span><span class="p">):</span>
    <span class="n">df</span><span class="o">.</span><span class="n">to_hdf</span><span class="p">(</span><span class="s1">&apos;test_fixed_compress.hdf&apos;</span><span class="p">,</span><span class="s1">&apos;test&apos;</span><span class="p">,</span><span class="n">mode</span><span class="o">=</span><span class="s1">&apos;w&apos;</span><span class="p">,</span><span class="n">complib</span><span class="o">=</span><span class="s1">&apos;blosc&apos;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">test_hdf_fixed_read_compress</span><span class="p">():</span>
    <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="s1">&apos;test_fixed_compress.hdf&apos;</span><span class="p">,</span><span class="s1">&apos;test&apos;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">test_hdf_table_write</span><span class="p">(</span><span class="n">df</span><span class="p">):</span>
    <span class="n">df</span><span class="o">.</span><span class="n">to_hdf</span><span class="p">(</span><span class="s1">&apos;test_table.hdf&apos;</span><span class="p">,</span><span class="s1">&apos;test&apos;</span><span class="p">,</span><span class="n">mode</span><span class="o">=</span><span class="s1">&apos;w&apos;</span><span class="p">,</span><span class="n">format</span><span class="o">=</span><span class="s1">&apos;table&apos;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">test_hdf_table_read</span><span class="p">():</span>
    <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="s1">&apos;test_table.hdf&apos;</span><span class="p">,</span><span class="s1">&apos;test&apos;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">test_hdf_table_write_compress</span><span class="p">(</span><span class="n">df</span><span class="p">):</span>
    <span class="n">df</span><span class="o">.</span><span class="n">to_hdf</span><span class="p">(</span><span class="s1">&apos;test_table_compress.hdf&apos;</span><span class="p">,</span><span class="s1">&apos;test&apos;</span><span class="p">,</span><span class="n">mode</span><span class="o">=</span><span class="s1">&apos;w&apos;</span><span class="p">,</span><span class="n">complib</span><span class="o">=</span><span class="s1">&apos;blosc&apos;</span><span class="p">,</span><span class="n">format</span><span class="o">=</span><span class="s1">&apos;table&apos;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">test_hdf_table_read_compress</span><span class="p">():</span>
    <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="s1">&apos;test_table_compress.hdf&apos;</span><span class="p">,</span><span class="s1">&apos;test&apos;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">test_csv_write</span><span class="p">(</span><span class="n">df</span><span class="p">):</span>
    <span class="n">df</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="s1">&apos;test.csv&apos;</span><span class="p">,</span><span class="n">mode</span><span class="o">=</span><span class="s1">&apos;w&apos;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">test_csv_read</span><span class="p">():</span>
    <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;test.csv&apos;</span><span class="p">,</span><span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
</div>
